JS 是基於物件的語言,可以藉由物件導向的思路去模擬 Java、C++ 這類語言的寫法。
面向過程關注的是「解決問題的步驟」;物件導向關注的則是「解決問題需要哪些角色」,先找出內容與角色,再依業務邏輯按規則呼叫相關方法。
物件分成系統物件與自訂物件兩類。系統物件可以透過呼叫內建模你都有的建構函式取得,例如 array、date;自訂物件則必須自己打造。
//直接創建 //JS創建對象 //1:創建空對象 var person1 = new Object(); //2:將該對象所需要的屬性、方法加進去 person1.name ="ailer" ; console .log(person1.name); person1.gender = "male"; //3:該對象添加的方法|(函數) person1. manager= function (){ console .log("Ailer is my English name" ); } //4:調用對象方法:對象.方法名(); person1.manager(); //函數|方法?函數屬於對象時,該函數屬於這個對象下的方法;通過方法名來調用該函數; //變量|屬性 ?當變量屬於某一個對象時候, 該變量就是這個對象下的方法。通過屬性名來調用變量。 //增 person1.age ="6" ; //改 person1.name ="lemon" ; //查 console .log(person1.name); //刪 delete person1.age; console .log(person1.age);==> undefined //引用類型,存儲的是地址 //基本類型:存儲的是值 標志位 /* var arr1 = [1,2,3,4] var arr2 = [5, 6, 7,9]; var arr2 = arr1;// arr2[0]=10;//更改arr2裡面的值,arr1也更改 alert(arr1[0]);//====>10 引用類型*/ var per2 = new Object(); per2.name = "Relia"; per2.age = "18"; per2.gender = "femal"; per2.hobby = "lemons"; //1:通過.(點語法)訪問屬性 //2:通過[](方括號)訪問對象的屬性;方括號中必須是屬性字符串或保存屬性字符串的變量|遍歷屬性的時候才使用方括號 var n = "name" //console.log(per2["name"]);//雙引號 console .log(per2[n]); for ( var property in per2) { // console.log(per2[property]); }
存取屬性有兩種寫法:點語法與中括號。中括號裡必須是字串或存放字串的變數,只有在遍历屬性時才非用它不可。這種寫法雖然直觀,但建立多個物件時程式碼會大量重複。
為了解決大量重複的問題,可以把建立流程包成一個函式:
//定義構造函數 function createPerson ( name, age) { //創建一個新的空對象 var person = new Object; //添加屬性|方法 person.name = name; person.age = age; person. manager = function() { console .log("ai" ); } //返回 return person; } var per1 = createPerson( "ailer", 12 ); console .log(per1.name); var per2 = createPerson( "lemon", 23 ); console .log(per2.age); console.log(per2 instanceof Object);//true console.log(per2 instanceof createPerson);//false//無法區分該對象類型 console.log(per2.manager==per1.manager);//false 可得出per1和per2各自開閉空間
優點是能批量產生同類實例;缺點是每個實例各自持有一份屬性與方法,造成記憶體浪費,而且 instanceof createPerson 會回傳 false,根本認不出物件類型。
優點是簡單直接,缺點顯然是無法批量產生同類物件。
//字面量創建的對象使用constructor屬性不會指向實例,而是指向object //使用字面量創建 var per1 = { name:"Ailer", constructor:per1, age:12, gender:"female", hobby:"play", eat:function(){ console.log(this.name); } } per1.eat();//ailer per1.name="lemon"; per1.eat();//lemon console.log(typeof per1);//Object console.log(per1.constructor==Object);//true
//構造函數創建對象,其子對象用instanceof不識別,所有采用new+obj創建 //對象識別了,但是仍然浪費一些代碼區;==>產生原型創建 //創建js對象 new+構造函數 //1:創建構造函數 |通常首字母大寫 function CreatePerson( name , age){ //2:把對象的屬性|方法掛靠在this指針身上, 當調用該函數創建對象時,this指針就指向這個新對象; //這個this就添加給這個對象 this.name = name; this.age = age; /*this.speak = function(){ //此處this也指向創建對象 console.log(this.name+" hello"); } } /* CreatePerson.prototype.gender = "20"; CreatePerson.prototype. ea = function(){ console .log(this.name+ "sfd" ); }*/ // __proto__:是:實例對象中的原型屬性, 指向對應構造函數對應的原型對象 // [[prototype]] //調用 var per1 = new CreatePerson( "ailer", "20" ); var per2 = new CreatePerson( "relia", "18" ); console .log(per1 instanceof CreatePerson); //==true console .log(per2 instanceof CreatePerson); //==>true console .log(per1.speak== per2.speak); //==false說明系統開辟了兩個不同的代碼區,造成了內存浪費.
這下型別可以被 instanceof 認出來了,但方法仍然每個實例一份,浪費依舊存在——於是就有了原型。
function CreateAnimal(name, age) { //1.2:把外部參數綁定實例屬性 this.name = name; this.age = age; } //1.3把相同的屬性,綁定在原型上(原型屬性,原型方法) CreateAnimal.prototype.gender = "male"; CreateAnimal.prototype.style = function() { console.log(this.name + " ailers"); }; //2:調用構造函數創建對象 var cat1 = new CreateAnimal("xiaohua", "2"); var cat2 = new CreateAnimal("xiaohua", "2"); cat1.style(); console.log(cat1.style==cat2.style);//方法引用地址一樣,將屬性放到原型對象中,節約地址 //instanceof可以來判斷對象屬於哪一個【函數】 //constructor 建造者 也可以用來判斷對象屬於哪個【構造函數】 【常】 //實例對象保存一個 constructor屬性指向它的構造函數 //instanceof and constructor 區別 console.log(cat1 instanceof CreateAnimal);//true console.log(cat1 instanceof Object);//true console.log(cat1.constructor == CreateAnimal);//true console.log(cat1.constructor == Object); //==false //構造函數的原型也有constructor屬性指會構造函數 console.log(CreateAnimal.prototype.constructor == CreateAnimal);//true //in判斷該屬性是否存在這個對象中,這個屬性為實例屬性或原型 // alert("name" in cat1)//true // alert("gender" in cat1);//true //hasOwnProperty:來判斷某一個屬性到底是實例屬性,還是繼承自原型屬性 if 是 為true, else不存在|不是返回false; console.log(cat1.hasOwnProperty("aaa"));//false 不存在的屬性返回為false console.log(cat1.hasOwnProperty("name"));//true 實例屬性 console.log(cat1.hasOwnProperty("style"));//false 原型屬性返回為false //遍歷屬性找原型屬性 //判斷參數是否為原型屬性 工具類 console.log(isPrototype("gender", cat1));//true function isPrototype(proString, obj) { if(proString in obj) { if(!obj.hasOwnProperty(proString)) { return true; } else { return false; } } else { return false; } } /* function isProperty(object, property) {//判斷原型中是否存在屬性 return !object.hasOwnProperty(property) && (property in object); }*/
把共用屬性與方法掛到 prototype 上之後,cat1.style == cat2.style 為 true,代表所有實例共用同一份函式,記憶體問題解決。搭配 instanceof、constructor、in 與 hasOwnProperty,就能清楚分辨某個屬性屬於實例還是原型。
把原型的初始化寫在建構函式裡,只在第一次執行時掛上,兼顧了兩種做法的優點:
//構造函數中初始化原型 function per(name, age, gender) { this.name = name; this.age = age; this.gender = gender; //只在初始化原型的時候執行一次 if(typeof this.sayName != "function") { Person.prototype.sayName = function() { alert(this.name); } } }
整理一下這條演進路線:字面量方便但無法批量,於是有了工廠模式;工廠模式型別認不出來且浪費記憶體,於是改用 new 加建構函式;new 仍有重複程式碼,於是引進原型;原型又把建構與定義拆成兩處,於是有了動態原型模式作為折衷。