JS原型學習筆記

來源:互聯網
上載者:User

標籤:

1.原型是函數對象的屬性,它的初始值是一個Null 物件,這個prototype原型對象可以添加方法和屬性。

2.構造器對象尋找屬性和方法時先尋找構造器後尋找原型。

3.若構造器中的屬性和原型中的屬性相同,構造器的優先順序會高於原型。

4.枚舉屬性(for-in)

(1).數組枚舉

var a=[1,2,3];

for(var i in a){

  console.log(a[i]);

}

(2).對象枚舉

var a={name:"gao",age:"18",sex:"male"};

for(var i in a){

  console.log(i+"="+a[i]);

}

 

5.構造器對象枚舉

function Obj(name,age){   

  this.name=name;  

  this.age=age;   

  this.getInfo=function(){    

    return "age:"+this.age+",name:"+this.name;   

  }  

}

Obj.prototype.score=121;  

Obj.prototype.scape="MS";  

Obj.prototype.getSc=function(){   

  return "score:"+this.score+".scope:"+this.scope;  

}

var me=new Obj("gx","17");

for(var prop in me){   

  if(me.hasOwnProperty(prop)){    

    console.log(prop+"="+me[prop]);   

  }  

}

 

6.hasOwnProperty()函數判斷屬性是否為構造器自身屬性

7.isPrototypeOf()方法,當前對象是否為另一個對象的原型

8.當對prototype對象進行重寫時,需重設constructor。

 

JS原型學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.