0

我只是在javascript中尝试了一些简单的东西

name = 'velu' 
fname = name
name = 'valu'
console.log(fname) // would still print as velu...

您如何处理生成的对象原型被更改并且生成的对象仍然具有旧对象的副本的情况。

这是情况...

function Cat(name){  
this.name = name  
}
var garfield = new Cat('Garfield')
Cat.prototype.greet = function(){
console.log('Meow, I am ' + this.name)
}
function Animal(){}
Cat.prototype = new Animal
Cat.prototype.constructor = Cat 
Animal.prototype.breed = function(){
console.log('Making a new animal!')
return new this.constructor()
}
 var kitty = garfield.breed() // this will not work as it garfield still is pointing to the old prototype object of Cat ...

谢谢

4

1 回答 1

3

您必须创建一个对象,该对象通过引用传递:

var name = { Value: 'velu' };
var fname = name;
name.Value = 'valu';
console.log(fname.Value);
于 2012-09-21T18:35:24.467 回答