DIV CSS 佈局教程網

JavaScript Object 類詳解:所有對象的根基
編輯:關於JavaScript     

在 JavaScript 中,Object 是所有對象的基類,也就是說,JS 裡的每一個對象都源自 Object。它主要用於把任意數據封裝成對象形式。本文將系統性地梳理 Object 類的屬性與方法。

一、Object 類介紹

Object 是所有 JavaScript 類的基類(父類),它提供了一種建立自訂對象的簡單方式,讓開發者不必自己定義構造函數。

二、Object 類的主要屬性

1. constructor:對象的構造函數。

2. prototype:取得類的 prototype 對象,屬於靜態性質。

三、Object 類的主要方法

1. hasOwnProperty(propertyName)

用來判斷對象是否具有某個特定屬性。呼叫時必須以字串指定屬性名,例如 obj.hasOwnProperty("name"),回傳布林值。要注意的是,這個方法不會檢查原型鏈上的屬性,屬性必須是對象本身的成員。

 var str =""; alert("str.hasOwnProperty(\"split\")的結果是:"+str.hasOwnProperty("split")); //return false alert("String.prototype.hasOwnProperty(\"split\")的結果是:"+String.prototype.hasOwnProperty("split"));//return true 

執行結果:

hasOwnProperty 的應用範圍其實很廣。以 jQuery 撰寫插件時,初始化參數是必經的一步,其中關鍵方法 $.extend() 背後正是利用了 hasOwnProperty()。它在 for-in 遍歷對象成員時,會判斷是否存在同名成員,若有就用新值覆蓋舊值。透過這種方式,我們就能藉由修改傳入參數來控制程式流程,而未被改動的部分則沿用預設值。我們也可以簡單模擬一下這個 extend 函數:

 function extend(target,source){//target 舊的 source新的 for (var i in source){ if(target.hasOwnProperty(i)){ target[i]=source[i]; } } return target; } var a={"first":,"second":"lyl","third":"bob"}; var b={"third":"leo"}; extend(a,b); for(var i in a){ alert(a[i]);//原本是bob,現在變成leo了 } 

2. isPrototypeOf(object)

用來判斷該對象是否為另一個對象的原型,語法為 obj1.isPrototypeOf(obj2)。其中 obj1 是一個對象實例,obj2 是要檢查原型鏈的對象。原型鏈可用來在同一類型的不同實例之間共享功能。如果 obj2 的原型鏈中包含 obj1,方法回傳 true;否則(obj2 不是對象,或 obj1 未出現在 obj2 的原型鏈中)回傳 false。

 <script type="text/javascript"> function foo(){ this.name = 'foo'; } function bar(){ } bar.prototype = new foo(); var goo = new bar(); alert(goo.name); //foo alert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型鏈中有當前對象goo,則isPrototypeOf方法返回true </script> 

3. propertyIsEnumerable(propertyName)

用來檢測某個對象成員是否可遍歷。若可遍歷,代表這個對象能透過 for-in 循環列舉,語法為 obj.propertyIsEnumerable(propertyName)。如果 propertyName 存在於 obj 中且能用 for-in 列舉出來,回傳 true;若 object 不具有該屬性,或該屬性不可列舉,則回傳 false。一般來說,預定義屬性不可列舉,而使用者自訂的屬性則總是可列舉的。

4. toString():回傳對象對應的字串。

5. valueOf():回傳對象對應的原始類型值。

以上五個方法都定義在 Object.prototype 上。由於 ECMAScript 中的所有對象都繼承自 Object,因此它們都具備這些方法。

測試程式碼一:

 var p = new Object(); //通過Object直接創建對象 //為p對象動態添加屬性 p.Age=; p.Name="孤傲蒼狼"; //擴展Object類,為Object類添加一個Show方法 Object.prototype.Show=function(){ alert(this.Age+"\t"+this.Name); } alert(p.Age); p.Show(); document.write("<pre>"); document.writeln("p.constructor:"+p.constructor);//得到對象的構造函數 document.writeln("Object.prototype:"+Object.prototype);//得到prototype對象,prototype是靜態屬性,只能通過"類名.prototype"去訪問 document.writeln("p.isPrototypeOf(p):"+p.isPrototypeOf(p)); document.writeln("p.hasOwnProperty(\"Age\"):"+p.hasOwnProperty("Age")); document.writeln("p.propertyIsEnumerable(\"Age\"):"+p.propertyIsEnumerable("Age")); document.writeln("p.toString():"+p.toString()); document.writeln("p.valueOf():"+p.valueOf()); document.write("</pre>"); 

執行結果:

測試程式碼二:

 var Car = function(){}; Car.prototype.hello = function(){ alert("hello car"); }; var car = new Car(); car.f = function() { alert("自定義方法"); } document.write("<pre>"); document.writeln("car.hasOwnProperty(\"f\")的結果是:"+car.hasOwnProperty("f"));//ture,car對象有f方法 document.writeln("car.propertyIsEnumerable(\"f\")的結果是:"+car.propertyIsEnumerable("f"));//ture,car對象有f方法,f方法是可以被枚舉的 document.writeln("car.hasOwnProperty(\"hello\")"+car.hasOwnProperty("hello")); // false,因為car本身沒有hello方法 document.writeln("car.propertyIsEnumerable(\"hello\")的結果是:"+car.propertyIsEnumerable("hello")); // false,沒有這個方法當然不能枚舉 document.writeln("car.constructor.prototype.hasOwnProperty(\"hello\")的結果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法 document.writeln("car.constructor.prototype.propertyIsEnumerable(\"hello\")的結果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的類的Car的原型hello方法是可以被枚舉的 document.writeln("Car.prototype.hasOwnProperty(\"hello\")的結果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法 document.writeln("Car.prototype.propertyIsEnumerable(\"hello\")的結果是:"+Car.prototype.propertyIsEnumerable("hello")); document.write("</pre>"); 

執行結果:

以上就是 JavaScript 中 Object 類的屬性與方法整理,希望對你理解 JS 對象體系有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved