一、Object類介紹
Object類是所有JavaScript類的基類(父類),提供了一種創建自定義對象的簡單方式,不再需要程序員定義構造函數。
二、Object類主要屬性
1.constructor:對象的構造函數。
2.prototype:獲得類的prototype對象,static性質。
三、Object類主要方法
1.hasOwnProperty(propertyName)
判斷對象是否有某個特定的屬性。必須用字符串指定該屬性,例如,obj.hasOwnProperty("name"),返回布爾值。此方法無法檢查該對象的原型鏈中是否具有該屬性;該屬性必須是對象本身的一個成員。
1 var str =""; 2 alert("str.hasOwnProperty(\"split\")的結果是:"+str.hasOwnProperty("split")); //return false 3 alert("String.prototype.hasOwnProperty(\"split\")的結果是:"+String.prototype.hasOwnProperty("split"));//return true
運行結果:
hasOwnProperty的用法不僅僅在此,在Jquery中在編寫插件中,少不了的一步,就是初始化參數,其中一個很重要的方法就是$.extend();他的原理就是應用了hasOwnProperty()方法;利用for in 循環遍歷對象成員中,有沒有相同名稱的對象成員,有的話就用這個新的對象成員替換掉舊的,通過這種方式,我們就可以通過修改方法中的參數變化,從而控制程序的流程,而對於那些沒有改變的部分,仍使用默認值進行控制,我們自己也可以簡單的模擬一下這個extend函數,如下
1 function extend(target,source){//target 舊的 source新的 2 for (var i in source){ 3 if(target.hasOwnProperty(i)){ 4 target[i]=source[i]; 5 } 6 } 7 return target; 8 } 9 var a1={"first":1,"second":"lyl","third":"bob"}; 10 var b1={"third":"leo"}; 11 extend(a1,b1); 12 for(var i in a1){ 13 alert(a1[i]);//原本是bob,現在變成leo了 14 }
2.isPrototypeOf(object)
判斷該對象是否為另一個對象的原型。
obj1.isPrototypeOf(obj2);
obj1是 一個對象的實例;obj2是另一個將要檢查其原型鏈的對象。原型鏈可以用來在同一個對象類型的不同實例之間共享功能。如果obj2的原型鏈中包含 obj1,那么isPrototypeOf 方法返回 true。如果obj2不是一個對象或者obj1沒有出現在obj2中的原型鏈中,isPrototypeOf 方法將返回 false。
1 <script type="text/javascript"> 2 function foo(){ 3 this.name = 'foo'; 4 } 5 function bar(){ 6 7 } 8 bar.prototype = new foo(); 9 var goo = new bar(); 10 alert(goo.name); //foo 11 alert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型鏈中有當前對象goo,則isPrototypeOf方法返回true 12 </script>
3.propertyIsEnumerable(propertyName)
通過這個方法我們可以檢測出這個對象成員是否是可遍歷的,如果是可遍歷出來的,證明這個對象就是可以利用for in 循環進行遍歷的,
格式如下:obj.propertyIsEnumerable(propertyName)
4.toString():返回對象對應的字符串
5.valueOf():返回對象對應的原始類型
以上5個方法都是Object.prototype上定義的,ECMAScript 中的所有對象都由Object繼承而來,所以在ECMAScript上的所有對象都具有以幾個方法
測試代碼1:
1 var p1 = new Object(); //通過Object直接創建對象 2 //為p1對象動態添加屬性 3 p1.Age=20; 4 p1.Name="孤傲蒼狼"; 5 //擴展Object類,為Object類添加一個Show方法 6 Object.prototype.Show=function(){ 7 alert(this.Age+"\t"+this.Name); 8 } 9 alert(p1.Age); 10 p1.Show(); 11 document.write("<pre>"); 12 document.writeln("p1.constructor:"+p1.constructor);//得到對象的構造函數 13 document.writeln("Object.prototype:"+Object.prototype);//得到prototype對象,prototype是靜態屬性,只能通過"類名.prototype"去訪問 14 document.writeln("p1.isPrototypeOf(p1):"+p1.isPrototypeOf(p1)); 15 document.writeln("p1.hasOwnProperty(\"Age\"):"+p1.hasOwnProperty("Age")); 16 document.writeln("p1.propertyIsEnumerable(\"Age\"):"+p1.propertyIsEnumerable("Age")); 17 document.writeln("p1.toString():"+p1.toString()); 18 document.writeln("p1.valueOf():"+p1.valueOf()); 19 document.write("</pre>");
運行結果:
測試代碼2:
1 var Car = function(){}; 2 Car.prototype.hello = function(){ 3 alert("hello car"); 4 }; 5 var car = new Car(); 6 car.f = function() { 7 alert("自定義方法"); 8 } 9 document.write("<pre>"); 10 document.writeln("car.hasOwnProperty(\"f\")的結果是:"+car.hasOwnProperty("f"));//ture,car對象有f方法 11 document.writeln("car.propertyIsEnumerable(\"f\")的結果是:"+car.propertyIsEnumerable("f"));//ture,car對象有f方法,f方法是可以被枚舉的 12 document.writeln("car.hasOwnProperty(\"hello\")"+car.hasOwnProperty("hello")); // false,因為car本身沒有hello方法 13 document.writeln("car.propertyIsEnumerable(\"hello\")的結果是:"+car.propertyIsEnumerable("hello")); // false,沒有這個方法當然不能枚舉 14 document.writeln("car.constructor.prototype.hasOwnProperty(\"hello\")的結果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法 15 document.writeln("car.constructor.prototype.propertyIsEnumerable(\"hello\")的結果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的類的Car的原型hello方法是可以被枚舉的 16 document.writeln("Car.prototype.hasOwnProperty(\"hello\")的結果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法 17 document.writeln("Car.prototype.propertyIsEnumerable(\"hello\")的結果是:"+Car.prototype.propertyIsEnumerable("hello")); 18 document.write("</pre>");
運行結果: