2015-05-09 107 views
0

如何將屬性添加到「new add()。city」和「add()。city」
是不同的。如何將屬性添加到函數不同於添加到函數構造函數

function add() 
{ 
    this.name = 'suresh'; 
    this.lastname = 'kumar'; 
} 

    var b = new add(); 
    b.city = 'coimbatore'; 

add.city = 'coimbatore'  // how this is different from a above statement 
+0

你問'add.city' VS'加()。現在city'? – Bergi

+0

好吧,'add!== b'。一個是函數,另一個是對象實例。 – Bergi

回答

1
  • 在第一個例子,你創建與add作爲 構造一個新的對象,並給它一個自己的財產

  • 在第二個例子,你給了add函數對象的 自己的財產

所以你在兩種情況下都做得幾乎相同。如果說,你改變了add的原型,那麼你會在所有使用new add()創建的對象中使該屬性「可繼承」。


function add() { 
    this.name = 'suresh'; 
    this.lastname = 'kumar'; 
} 

var b = new add(); 

// Adds property to the new b object, whose constructor is add. 
// Only b will have this property. 
b.city = 'coimbatore'; 

// Adds property ONLY to the function object. 
// Objects created with add as a constructor won't 'inherit' it 
// Only `add` will have this property (although in this case, 
// b has a property named the same way, with the same value, 
// but they are own properties of each object). 
add.city = 'coimbatore'; 

// Adds property to add's prototype. Objects created with `add` 
// as a constructor will inherit it. (for instance `var z = new add()`) 

add.prototype.hello = 'there'; 

console.log(b.hello); // 'there' 

var c = new add(); 
console.log(c.hello); // 'there' 

// Adds own property to the `c` object. 
// Only c will have this property: 

c.myVeryOwnProperty = 'yeaah!'; 
console.log(c.myVeryOwnProperty); // 'yeaah!' 
console.log(b.myVeryOwnProperty); // undefined 
console.log(add.myVeryOwnProperty); // undefined 

// Now, check this out: 

// True, because you set the properties directly on the object 
console.log(add.hasOwnProperty('city')); // true 
console.log(b.hasOwnProperty('city')); // true 
console.log(c.hasOwnProperty('myVeryOwnProperty')); // true 

// False, because these objects got the `hello` property 
// through add's prototype 
console.log(b.hasOwnProperty('hello')); // false 
console.log(c.hasOwnProperty('hello')); // false 
+0

函數對象?它有什麼默認屬性。它有爭論,名字嗎? –

+0

是的,函數是對象,你可以像這樣對待它們。嘗試'add.name',它會記錄''add''。它們還有其他一些屬性,比如'length',它表示函數有多少正式參數。 – 2015-05-09 16:29:54

+0

所以函數體是函數構造函數,所以函數體中的屬性變成了「var b = new add()」的屬性。 –

1

證明的差異最簡單的方法是通過從你的榜樣的線路之一,看着我們所擁有的

function add() { 
    this.name = 'suresh'; 
    this.lastname = 'kumar'; 
} 

var b = new add(); 
// b.city = 'coimbatore'; // this line removed 

add.city = 'coimbatore'; 

現在,什麼是b.city

b.city; // undefined 

沒有未來的實例都會有一個城市的性質要麼,所以它不是設置的只是順序導致b沒有擁有它。

var c = new add(); 
c.city; // undefined 

這意味着設置在構造一個屬性上有構造的對象沒有影響(有一些特殊的例外,如原型屬性)


發生這種情況,因爲add構造函數是它自己的對象new add創建的對象是實例add,所以繼承自add.prototype而不是add本身。


如果你想每個實例繼承的東西,如果他們不自己的屬性它的好,那麼你可以將它們添加到構造函數的原型。要小心,如果你添加對象雖然任何更改將修改爲所有實例。

考慮

add.prototype.city = 'coimbatore'; 
b.city; // "coimbatore" 
// and don't have to worry if you change it on an instance 
b.city = 'fizz'; 
c.city; // "coimbatore" 

而且危險我警告與對象

add.prototype.foo = {bar: 'baz'}; 
b.foo.bar; // "baz" 
// but have to worry if you change it on an instance 
b.foo.bar = 'fizz'; 
c.foo.bar; // "fizz", it got changed too :(