以前再取子字串時都習慣使用 substr
今天才知道這個 function 不算是標準的 ECMAScript Spec(僅在附錄)
故建議是不要使用 substr
可以用 substring or slice 來代替
Annex B (normative)
Additional ECMAScript Features for Web Browsers
The ECMAScript language syntax and semantics defined in this annex are required when the ECMAScript host is a web browser. The content of this annex is normative but optional if the ECMAScript host is not a web browser.
NOTE This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex defined the requirements for interoperable implementations of these legacy features.
These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.
pretty code
2018年11月1日 星期四
Javascript 浮點數轉整數
Javascript 的數字類型是 64 位元的浮點數
以前在轉 C code 時
遇到類似 3/2 的語法都用 parseInt(3/2) 來取代
正規來說,應該要用 Math.floor(3/2)
也讓不懂 Javascript 的人比較好懂 code 的意義
以前在轉 C code 時
遇到類似 3/2 的語法都用 parseInt(3/2) 來取代
正規來說,應該要用 Math.floor(3/2)
也讓不懂 Javascript 的人比較好懂 code 的意義
2018年10月31日 星期三
Javascript variable hoisting
以前只是大概知道
並沒有認真搞懂
使用 var 宣告的變數 scope
是在最接近它的 function 裡
故在 a 處,因為 variable hoisting 的關係
x 變數是存在的,但是仍未賦值
故印出來的結果是 undefined
假設我們沒有宣告 var x = 2
則 a 處就會觸發 "ReferenceError: x is not defined"
並沒有認真搞懂
使用 var 宣告的變數 scope
是在最接近它的 function 裡
故在 a 處,因為 variable hoisting 的關係
x 變數是存在的,但是仍未賦值
故印出來的結果是 undefined
假設我們沒有宣告 var x = 2
則 a 處就會觸發 "ReferenceError: x is not defined"
function foo() {
console.log(x); // a
var x = 2;
console.log(x);
}
foo();
2018年10月25日 星期四
GPL 之我見
GPL 對寫程式的人應該都很不陌生
一般來說,用到 GPL 程式碼的後續程式
理論上都會視為 GPL 的感染
那如果開發者只是單純的使用 GPL 的程式呢?
假設說是資料庫好了
比如說 MariaDB 是 GPL 授權
我寫了一個 IoT Gateway 的 message collection system
我使用了 MariaDB 來儲存資料
這時我的整個 system 的程式是否也變成 GPL 呢?
以前曾經聽過一種說法(僅針對 GPLv2)
如果你可以證明你的 system 不需要使用 MariaDB 也可以單獨執行
那就可以不用變成 GPL
故你的 system 需確保這件事
因為 SQLite 是使用 Public Domain 的授權
只要你的 system 沒有 MariaDB 也可以使用 SQLite 正常運作
一般來說就會視為跟 MariaDB 間是獨立的關係
當然如果你選擇在安裝包中包含 MariaDB
你必須準備好 MariaDB 相關的文件與程式碼
2018/10/29 Update
即使你的作品符合上述獨立關係的定義
但當你將 MariaDB 和你的作品一起發佈時
整個作品都會受到 GPL 之約束
GPL 對其他許可獲得者的授權將延伸至整個作品
不論作品是誰寫的
(These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.)
以上是我個人的理解
故最安全的方式應該是將作品和 GPL 的元件分開發佈
今天在林誠夏先生的 "GPL 條款對於衍生程式的判定標準與其授權拘束性的擴散範圍(上)" 這篇文章看到這段
GPL 授權條款也明文表達了一個授權拘束性的例外規定,那就是、具有「獨立性與可區分性(Separate and Independent,註三)」的軟體元件,並不會因為僅與 GPL 授權元件同在一個軟體專案的架構下運作,就被歸類為 GPL 授權元件的衍生著作。所以、若是符合「Separate and Independent」這個例外條件,此時軟體專案便可以被認定為一個統合的聚合作品 (aggregation),此時散布整個聚合的軟體專案時,就只需要提供該 GPL 授權元件的程式源碼,而不一定要將 GPL 元件的授權拘束性擴及到其他自行編寫且獨立運作的個別元件。那麼、接下來的問題就是,所謂的「獨立性與可區分性」,是不是已經有一個公定標準,或是多數 GPL 授權元件的開發者皆已得到共識的參考範圍?
應該有符合我以前聽過的說法吧?
相關連結
http://blog.ez2learn.com/2011/11/25/taiwan-software-lacking-of-open-source/
一般來說,用到 GPL 程式碼的後續程式
理論上都會視為 GPL 的感染
那如果開發者只是單純的使用 GPL 的程式呢?
假設說是資料庫好了
比如說 MariaDB 是 GPL 授權
我寫了一個 IoT Gateway 的 message collection system
我使用了 MariaDB 來儲存資料
這時我的整個 system 的程式是否也變成 GPL 呢?
以前曾經聽過一種說法(僅針對 GPLv2)
如果你可以證明你的 system 不需要使用 MariaDB 也可以單獨執行
那就可以不用變成 GPL
故你的 system 需確保這件事
因為 SQLite 是使用 Public Domain 的授權
只要你的 system 沒有 MariaDB 也可以使用 SQLite 正常運作
一般來說就會視為跟 MariaDB 間是獨立的關係
2018/10/29 Update
即使你的作品符合上述獨立關係的定義
但當你將 MariaDB 和你的作品一起發佈時
整個作品都會受到 GPL 之約束
GPL 對其他許可獲得者的授權將延伸至整個作品
不論作品是誰寫的
(These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.)
以上是我個人的理解
故最安全的方式應該是將作品和 GPL 的元件分開發佈
今天在林誠夏先生的 "GPL 條款對於衍生程式的判定標準與其授權拘束性的擴散範圍(上)" 這篇文章看到這段
GPL 授權條款也明文表達了一個授權拘束性的例外規定,那就是、具有「獨立性與可區分性(Separate and Independent,註三)」的軟體元件,並不會因為僅與 GPL 授權元件同在一個軟體專案的架構下運作,就被歸類為 GPL 授權元件的衍生著作。所以、若是符合「Separate and Independent」這個例外條件,此時軟體專案便可以被認定為一個統合的聚合作品 (aggregation),此時散布整個聚合的軟體專案時,就只需要提供該 GPL 授權元件的程式源碼,而不一定要將 GPL 元件的授權拘束性擴及到其他自行編寫且獨立運作的個別元件。那麼、接下來的問題就是,所謂的「獨立性與可區分性」,是不是已經有一個公定標準,或是多數 GPL 授權元件的開發者皆已得到共識的參考範圍?
應該有符合我以前聽過的說法吧?
相關連結
http://blog.ez2learn.com/2011/11/25/taiwan-software-lacking-of-open-source/
2018年10月18日 星期四
go-seo SEO 小工具
之前就很想幫 blog 某些文章增加搜尋度
故研究了一下要如何實現
後來發現只要下的關鍵字可以在 google 結果中找到 blog 頁面
這時再去點取該 blog 頁面
Blogger 系統便會在該 blog 頁面瀏覽次數加 1
於是就達成了我的目標
如此一來,我最受歡迎的頁面應該就不是大台北行政區圖了吧
相關程式請詳
https://github.com/tylpk1216/go-seo
故研究了一下要如何實現
後來發現只要下的關鍵字可以在 google 結果中找到 blog 頁面
這時再去點取該 blog 頁面
Blogger 系統便會在該 blog 頁面瀏覽次數加 1
於是就達成了我的目標
如此一來,我最受歡迎的頁面應該就不是大台北行政區圖了吧
相關程式請詳
https://github.com/tylpk1216/go-seo
2018年10月16日 星期二
Javascript async / await 速成
01. await 後面接的是 1 個 Promise,一定會等待此 Promise 狀況 ready
02. new Promise((resolve, reject) => {
// ok
resolve(xx);
return;
// error
reject(new Error("xxx"));
});
03. async function 一定要回傳 1 個 Promise
async function asyncXXX(xx) {
try {
// return value, 會被隱性轉成 Promise
return 1;
// Use static method
return Promise.resolve(xx);
} catch(err) {
return Promise.reject(err);
}
}
asyncXXX(xx).then((xx) => {
console.log(xx);
// 如果有 return Promise 會形成 Promise chain
return 1;
return new Promise(...);
}).catch((err) => {
// err is Error type
console.log(err.message);
});
04. Promise 只是確保在 Promise 控制的範圍內可以照順序
在呼叫 Promise 時 本身還是非同步
故底下會先印出 "after" 而不是 "in then"
async function test() {
return Promise.resolve();
}
test.then(() => {
console.log("in then");
});
console.log("after");
05. async / await 只是語法糖,骨子裡還是 Promise 的運用
2018年10月5日 星期五
Copy object in Javascript
在 Javascript 中,複製 1 個 object,只是類似做 1 個 reference
故當修改了外面傳進來的 object 後
原先外面的那個 object 也會被修改
故我們要用底下第 2 種寫法以避免 bug
故當修改了外面傳進來的 object 後
原先外面的那個 object 也會被修改
故我們要用底下第 2 種寫法以避免 bug
var newDataArg = dataArg;
newDataArg.xxx = "xxxx"; // wrong - dataArg is modified.
var newDataArg = Object.assign({}, dataArg);
newDataArg.xxx = "xxxx";
訂閱:
文章 (Atom)