2015-09-04 79 views
2

一些問題,如果我有JavaScript函數:關於JavaScript的原型和構造

function Person(){} 
  1. 這之後是什麼Person.prototype價值?

  2. Person.constructorPerson.prototype.constructor有什麼區別?

如果我的代碼:

function Student(){} 
Student.prototype = new Person() 
Student.prototype.constructor = Student 
  • 爲什麼從人都要我設置Student.prototype.constructorStudent繼承學生?
  • 回答

    0
    1. 這之後是什麼Person.prototype的價值?

    與指向功能對象中的屬性constructor的對象。

    1. Person.constructor和Person.prototype.constructor有什麼不同?

    一個是功能對象的一部分,而另一個是將用於鏈原型原型對象的一部分。在Person上使用new操作數時,此新對象不會繼承constructor

    function Student(){} 
    Student.prototype = new Person() 
    Student.prototype.constructor = Student 
    

    您正在創建Person實例並將其指定爲學生原型。通過這樣做,您也繼承了Person的構造函數,您可能需要使用Student的構造函數。

    通過創建Person的新對象,您也可以指定構造函數。否則,如果你完成了Student.prototype = Person.prototype然後Student.prototype.constructor = Student你也會將Person.prototype.constructor更改爲Student

    您仍在調用您正在使用newfunction的構造函數。

    0

    的回答你的問題在微詳細說明,以便有看看這篇文章,。我用我所有的答案那篇文章

    在此之後,最好的文章Prototype它的一個價值是什麼Person.prototype的?

    function Person(){} 
    

    每當一個函數的定義,它有一個名爲*原型這是不可枚舉的屬性。

    這個原型屬性是一個對象具有非叫構造枚舉屬性,它是配置

    此構造僅僅是函數本身是在這種情況下 所以,如果你檢查Person.prototype.constructor == PersonPerson.constructor == Person參考。它會返回true。 (後面的一個與第一個表達相同,由財產代表團)這回答你的第二個問題。

    Person.constructor和Person.prototype.constructor和有什麼區別?

    Javascript屬性委託,當你嘗試訪問一個不存在於對象中的屬性時,它將嘗試檢查其原型鏈接。

    當您嘗試訪問Person.prototype對象上名爲x的屬性時。

    它將首先檢查Person.prototype,因爲沒有名爲x的屬性。它會檢查父母。這Object.prototype中(其中所有的對象原型。這將是你的祖先鏈最後的祖先)

    第三個問題是關於建立子類或對象繼承Person類

    通過默認的學生函數有原型對象。執行此語句時,new Person()將創建一個對象,並將創建的對象的引用傳遞給Person.call(this),並將對象原型鏈接到 Person.prototype。 Student.prototype = new Person() 此方法用於模仿Subclass繼承。這樣從* new學生()創建的新對象也可以訪問類方法和變量通過原型鏈接。

    Student.prototype.constructor = Student

    的需要這種說法是當U創建一個新的Student對象檢查繼承,使得新對象的instanceof學生班級

    var bob = new Student()

    所以當u要檢查即bob instanceof學生。在後臺,這將做bob.constructor == Student。由於bob沒有構造函數屬性,它將委派並簽入Student。

    如果你的頭腦仍然不清楚這個答案。請參考上面的鏈接。關於原型

    0
    function Person(){} 
    
    1. 什麼是Person.prototype的價值?

      • 當一個函數被創建時,prototype屬性被設置爲該函數的空對象。
      • 當創建該函數的一個實例時,這個原型屬性(空對象)開始發揮作用。
      • 對於新創建的實例,
        instance.__proto__ or Object.prototypeOf(instance) would return the Person.prototype
      • 基本上,創建的實例的父Person.prototype的
    2. Person.constructor和Person.prototype.constructor

      • 這兩個是完全不同的。開始,
        constructor is not Person's property, it is inherited from Person.__proto__.
      • 當使用Person創建實例時,Person.prototype.constructor再次發揮作用。使用的人創建的所有實例將共享相同的構造函數,也不過是人函數本身
      • 我們會碰到循環引用,如果我們繼續
        Person.prototype.constructor.prototype..... This is because, Person.protoype.constructor refers to Person function itself
    3. 爲什麼設置Student.prototype.constructor =學生去?

      • 案例1:沒有設定構造

        • s = new Student(); 
          s.constructor // returns function Person(){}
        • 這是因爲,在創建實例時,實例將不會自己constructor屬性,它只是從原型繼承,在這種情況下,原型是Person對象,所以,
          s.constructor which is s.prototype.constructor would show function Person(){}
      • 情況2:通過設置構造函數

        • 當Student.prototype.constructor設置爲Student時,創建的所有實例都將在檢查s.prototype.constructor或s.constructor時正確顯示函數Student(){}。