JavaScript物件導向深入理解原型

來源:互聯網
上載者:User

標籤:this   log   job   strong   簡單的   tor   帶來   建立   會同   

 原型模式

function Person(){}Person.prototype.name="Ewarm";Person.prototype.age="29";Person.prototype.job="software Engineer";Person.prototype.sayName=function(){alert(this.name);}var person1=new Person()//我們可以通過isPrototypeOf()方法來確定對象之間是否存在這種關係,即執行個體.__proto__是否指向建構函式的原型對象,如果指向那麼這個方法返回為ture。alert(Person.prototype.isPrototypeOf(person1))//true//ECMAScript5增加了一個新方法 Object.getPrototypeOf() 這個方法返回的是執行個體.__proto__的值,即建構函式原型對象alert(Object.getPrototypeOf(person1)==Person.prototype)//true 返回執行個體.__proto__的值alert(Object.getPrototypeOf(person1).name) //Ewarm

  

//細心的你們會發現這樣每次都是Person.prototype.屬性真的好麻煩於是我們出現下面這種

 更簡單的原型文法

function Person(){}Person.prototype={name:"Ewarm",age:25,job:"software engineer",sayName:function(){alert(this.name)}}//注意這裡雖然和之前的最終結果相同 但是這裡的constructor屬性不再指向Person了 原因:每建一個函數,就會同時建立它的prototype對象,這個對象也會自動獲得constructor屬性,此時通過constructor已經無法確定物件類型了var person=new Person()alert(person instanceof Object )//turealert(person instanceof Person)//turealert(person.constructor==Person)//falsealert(person.constructor==Object)//true

  

//但是我們可以通過以下方法來修正constructor

function(){}Person.prototype={constructor:Person,//相當於複寫name:"Ewarm",age:25,job:"software engineer",sayName:function(){alert(this.name)}}

//這樣雖然修正了但是,constructor的enmurable屬性會變為true 即可枚舉 如果要設定回去 可以使用Object.defineProperty(),只適用於ECMAScript5相容器的瀏覽器

原型的動態性

function Person(){}var person=new Person()Person.prototype={constructor:Person,//相當於複寫name:"Ewarm",age:25,job:"software engineer",sayName:function(){alert(this.name)}}person.sayName()//error

//報錯的原因:重寫原型對象切斷了現有原型與任何之前已經存在的執行個體之間的聯絡,他們引用的仍然是最初的原型

function Person(){}var person1=new Person()Person.prototype.sayHi=function(){alert("hi")}person1.sayHi()//hi//這裡相當於添加方法而非重寫  

 往原生對象添加方法

String.prototype.Ewarm=function(text){return this.indexOf(text)==0//找到為0}var message="hello Ewarm"alert(message.Ewarm("hello"))//true 

 原型的問題
//共用所帶來的問題 尤其是針對參考型別值的屬性

function Person(){}Person.prototype={constructor:Person,name:"Ewarm",age:25,job:"software engineer",friends:["cch","emon"],sayName:function(){alert(this.name)}}var person1=new Person()var person2=new Person()person1.friends.push("tea")alert(person1.friends)//"cch","emon","tea"alert(person2.friends)//"cch","emon","tea"alert(person1.friends==person2.friends)//true//組合使用建構函式模式和原型模式可以解決這個問題

 如果看不懂 建議挨個敲一遍哦~ 這樣更容易理解 

JavaScript物件導向深入理解原型

聯繫我們

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