欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript知識(shí)點(diǎn)總結(jié)(十一)之js中的Object類詳解

 更新時(shí)間:2016年05月31日 16:36:55   作者:孤傲蒼狼  
這篇文章主要介紹了JavaScript知識(shí)點(diǎn)總結(jié)(十一)之js中的Object類詳解的相關(guān)資料,需要的朋友可以參考下

JavaScript中的Object對(duì)象,是JS中所有對(duì)象的基類,也就是說(shuō)JS中的所有對(duì)象都是由Object對(duì)象衍生的。Object對(duì)象主要用于將任意數(shù)據(jù)封裝成對(duì)象形式。

一、Object類介紹

  Object類是所有JavaScript類的基類(父類),提供了一種創(chuàng)建自定義對(duì)象的簡(jiǎn)單方式,不再需要程序員定義構(gòu)造函數(shù)。

二、Object類主要屬性

  1.constructor:對(duì)象的構(gòu)造函數(shù)。

  2.prototype:獲得類的prototype對(duì)象,static性質(zhì)。

三、Object類主要方法

  1.hasOwnProperty(propertyName)

  判斷對(duì)象是否有某個(gè)特定的屬性。必須用字符串指定該屬性,例如,obj.hasOwnProperty("name"),返回布爾值。此方法無(wú)法檢查該對(duì)象的原型鏈中是否具有該屬性;該屬性必須是對(duì)象本身的一個(gè)成員。

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

運(yùn)行結(jié)果:

  

  hasOwnProperty的用法不僅僅在此,在Jquery中在編寫(xiě)插件中,少不了的一步,就是初始化參數(shù),其中一個(gè)很重要的方法就是$.extend();他的原理就是應(yīng)用了hasOwnProperty()方法;利用for in 循環(huán)遍歷對(duì)象成員中,有沒(méi)有相同名稱的對(duì)象成員,有的話就用這個(gè)新的對(duì)象成員替換掉舊的,通過(guò)這種方式,我們就可以通過(guò)修改方法中的參數(shù)變化,從而控制程序的流程,而對(duì)于那些沒(méi)有改變的部分,仍使用默認(rèn)值進(jìn)行控制,我們自己也可以簡(jiǎn)單的模擬一下這個(gè)extend函數(shù),如下

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,現(xiàn)在變成leo了
} 

  2.isPrototypeOf(object)

  判斷該對(duì)象是否為另一個(gè)對(duì)象的原型。

  obj1.isPrototypeOf(obj2);

  obj1是 一個(gè)對(duì)象的實(shí)例;obj2是另一個(gè)將要檢查其原型鏈的對(duì)象。原型鏈可以用來(lái)在同一個(gè)對(duì)象類型的不同實(shí)例之間共享功能。如果obj2的原型鏈中包含 obj1,那么isPrototypeOf 方法返回 true。如果obj2不是一個(gè)對(duì)象或者obj1沒(méi)有出現(xiàn)在obj2中的原型鏈中,isPrototypeOf 方法將返回 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的原型鏈中有當(dāng)前對(duì)象goo,則isPrototypeOf方法返回true
</script> 

  3.propertyIsEnumerable(propertyName)

  通過(guò)這個(gè)方法我們可以檢測(cè)出這個(gè)對(duì)象成員是否是可遍歷的,如果是可遍歷出來(lái)的,證明這個(gè)對(duì)象就是可以利用for in 循環(huán)進(jìn)行遍歷的,

  格式如下:obj.propertyIsEnumerable(propertyName)

  如果 propertyName存在于 obj中且可以使用一個(gè) For…In 循環(huán)窮舉出來(lái),那么 propertyIsEnumerable 屬性返回 true。如果 object 不具有所指定的屬性或者所指定的屬性不是可列舉的,那么 propertyIsEnumerable 屬性返回 false。典型地,預(yù)定義的屬性不是可列舉的,而用戶定義的屬性總是可列舉的。

  4.toString():返回對(duì)象對(duì)應(yīng)的字符串

  5.valueOf():返回對(duì)象對(duì)應(yīng)的原始類型

  以上5個(gè)方法都是Object.prototype上定義的,ECMAScript 中的所有對(duì)象都由Object繼承而來(lái),所以在ECMAScript上的所有對(duì)象都具有以幾個(gè)方法

測(cè)試代碼1:

var p = new Object(); //通過(guò)Object直接創(chuàng)建對(duì)象
//為p對(duì)象動(dòng)態(tài)添加屬性
p.Age=;
p.Name="孤傲蒼狼";
//擴(kuò)展Object類,為Object類添加一個(gè)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);//得到對(duì)象的構(gòu)造函數(shù)
document.writeln("Object.prototype:"+Object.prototype);//得到prototype對(duì)象,prototype是靜態(tài)屬性,只能通過(guò)"類名.prototype"去訪問(wèn)
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>"); 

運(yùn)行結(jié)果:

測(cè)試代碼2:

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\")的結(jié)果是:"+car.hasOwnProperty("f"));//ture,car對(duì)象有f方法
document.writeln("car.propertyIsEnumerable(\"f\")的結(jié)果是:"+car.propertyIsEnumerable("f"));//ture,car對(duì)象有f方法,f方法是可以被枚舉的
document.writeln("car.hasOwnProperty(\"hello\")"+car.hasOwnProperty("hello")); // false,因?yàn)閏ar本身沒(méi)有hello方法
document.writeln("car.propertyIsEnumerable(\"hello\")的結(jié)果是:"+car.propertyIsEnumerable("hello")); // false,沒(méi)有這個(gè)方法當(dāng)然不能枚舉
document.writeln("car.constructor.prototype.hasOwnProperty(\"hello\")的結(jié)果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法
document.writeln("car.constructor.prototype.propertyIsEnumerable(\"hello\")的結(jié)果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的類的Car的原型hello方法是可以被枚舉的
document.writeln("Car.prototype.hasOwnProperty(\"hello\")的結(jié)果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的類Car的原型有hello方法
document.writeln("Car.prototype.propertyIsEnumerable(\"hello\")的結(jié)果是:"+Car.prototype.propertyIsEnumerable("hello"));
document.write("</pre>"); 

運(yùn)行結(jié)果:

  

以上所述是小編給大家介紹的JavaScript知識(shí)點(diǎn)總結(jié)(十一)之js中的Object類詳解,希望對(duì)大家有所幫助

相關(guān)文章

最新評(píng)論