JavaScript知識點總結(十一)之js中的Object類詳解_javascript技巧

來源:互聯網
上載者:User

JavaScript中的Object對象,是JS中所有對象的基類,也就是說JS中的所有對象都是由Object對象衍生的。Object對象主要用於將任意資料封裝成對象形式。

一、Object類介紹

  Object類是所有JavaScript類的基類(父類),提供了一種建立自訂對象的簡單方式,不再需要程式員定義建構函式。

二、Object類主要屬性

  1.constructor:對象的建構函式。

  2.prototype:獲得類的prototype對象,static性質。

三、Object類主要方法

  1.hasOwnProperty(propertyName)

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

var str ="";alert("str.hasOwnProperty(\"split\")的結果是:"+str.hasOwnProperty("split")); //return falsealert("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,那麼isPrototypeOf 方法返回 true。如果obj2不是一個對象或者obj1沒有出現在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); //fooalert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型鏈中有當前對象goo,則isPrototypeOf方法返回true</script> 

  3.propertyIsEnumerable(propertyName)

  通過這個方法我們可以檢測出這個對象成員是否是可遍曆的,如果是可遍曆出來的,證明這個對象就是可以利用for in 迴圈進行遍曆的,

  格式如下:obj.propertyIsEnumerable(propertyName)

  如果 propertyName存在於 obj中且可以使用一個 For…In 迴圈窮舉出來,那麼 propertyIsEnumerable 屬性返回 true。如果 object 不具有所指定的屬性或者所指定的屬性不是可列舉的,那麼 propertyIsEnumerable 屬性返回 false。典型地,預定義的屬性不是可列舉的,而使用者定義的屬性總是可列舉的。

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

  5.valueOf():返回對象對應的原始類型

  以上5個方法都是Object.prototype上定義的,ECMAScript 中的所有對象都由Object繼承而來,所以在ECMAScript上的所有對象都具有以幾個方法

測試代碼1:

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>"); 

運行結果:

測試代碼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\")的結果是:"+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知識點總結(十一)之js中的Object類詳解,希望對大家有所協助

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.