2015-05-09 50 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 :(