🏆 超過70萬名讀者已閱讀本站的內容,共同創造了105萬次總閱讀次數!
1. Functions are objects
* function 是一種 object, 所以它也有 built-in property 或是動態設定 property.
* function 可以當 input argument, 也可以當 return variable.
2. Function scope
* nested function 子層級的 function 可以看到父層級 function 的 variable, PHP 不行,PHP 下層 scope 也看不到 global variable, 除非用 global keyword.
3. Hoisting
* 變數或 function 宣告都自動提升到該 scope 的上方。注意提升到上方的變數初始為 undefined, 所以如果不是一開始就初始的變數,在初始之前是 undefined.
* 一般 function 宣告會 hoisting,但如果是 assign function as a variable, 一樣,該 variable 會 hoisting 到上方初始為 undefined.
* 這是 JS 和一般 OO 語言最大的不同之一,沒有 class,而且 object 可以動態新增 property.
5. Constructors and prototypes
* JS 的 constructor 是 “constructor function”, PHP 是 (double underscore)
function __construct() {
}
* JS 的繼承實作
function Animal() {
}
function Dog() {
}
Dog.prototype = new Animal();
注意子類別新的 property 不會出現在 prototype 裡 (ex: Dog.prototype)。另外子類別可以複寫父類別的 property (constructing 順序的問題)。
* JS ECMAScript 6 有導入 class, constructor, extends 等 keyword.
注意子類別新的 property 不會出現在 prototype 裡 (ex: Dog.prototype)。另外子類別可以複寫父類別的 property (constructing 順序的問題)。
* JS ECMAScript 6 有導入 class, constructor, extends 等 keyword.
我是 Larry,經營 B2B 軟體公司超過 10 年,也持續寫作超過 15 年。
我會持續分享自己對商業、科技與生活的觀察。
如果你喜歡這篇文章,也歡迎追蹤我的社群:
📌 Threads|日常觀察與商業思考
📌 FB|文章與生活分享
📌 IG|旅行、美食短影片







