物件導向程式設計 (Object-oriented programming, OOP) 是一種使用抽象概念表達現實世界的程式設計方式。 例如,一個類別可以對應一個名詞。
JavaScript 是一個以Prototype-based OOP)幾個特徵是函數來當做類別 (Class) 的建構子 (Constructor)。即使,現在的javascript也有關鍵字class 但是,仍然只是翻譯
成函數建構子的一個包裝而已。
Namespace
可讓開發人員包裝所有功能到一個獨一無二、特定應用程式名稱的容器。
Class 類別
Object 類別 (Class) 的實際案例。
Property 類別的feature (對照欄位)。例如:顏色rectangle.color=red。
Method 類別的行為(對照函數) 例如:rectangle.draw() 。
Encapsulation:看的見/看不見的函數和欄位。
Constructor
Inheritance
Abstraction: 通常用來作為最上層的規格。(類似的觀念有interface)
Polymorphism
Object.create(): the New Way to Create Objects in JavaScript
Data Structures: Objects and Arrays :chapter 4
本文:[JS] 06 Object.docx
本文:[JS] 06 Object tree.docx
本文:[JS] 06 prototype.pptx
function xx(){ d: true; } function x1(){ this.d=true; } var o1=new x1;
問:
typeof xx
typeof x1
typeof o1
分別給出什麼答案?
答案:
typeof xx ==> "function"
typeof x1 ==> "function"
typeof o1 ==> "object"
class User { constructor(name) { this.name = name; } sayHi() { alert(this.name); } }
hint:
typeof(User) ==> 'function'