2012-04-27 175 views
5

请帮我理解这段代码。Javascript对象继承

var person = { 
    'first-name': 'FirstName', 
    'last-name': 'LastName', 
    'gender': 'Male' 
}; 

var anotherPerson = new Object(person); 
anotherPerson.desig = 'Designation'; 

console.log('Another person designation: ' + anotherPerson['desig'] + ', person designation: ' + person['desig']); 

我预计输出为Another person designation: Designation, person designation: undefined,但让我吃惊,我发现它是`Another person designation: Designation, person designation: Designation

据我anotherPerson正在扩大person对象和属性设置为anotherPerson应该是不可见的person对象。我错了吗?或者是这两个对象都指向相同的位置?

[编辑]

现在甚至有更多的惊喜。

我将以下代码添加到上面。

person.place = 'XYZ'; 
console.log(person['place'] + ', ' + anotherPerson['place']); // Expected: XYZ, undefined. Result: XYZ, XYZ. 

基于上述结果和答案,我认为这两个对象都指的是相同的位置。现在我又增加了几行

person = undefined; 
console.log(anotherPerson['place']) //Expected: error, Result: XYZ. ??!?!? 
console.log(person['place']) // Expected: error, Result: error. 

有人可以告诉我一些明白这一点吗? 感谢您的帮助提前

+0

这里没有继承。只有两个引用同一个对象。 – Cameron 2012-04-27 19:59:51

+0

那你怎么克隆这个对象并制作一个新对象呢?我和OP一样,可能会认为新建立了一个NEW对象。 – DanRedux 2012-04-27 20:01:36

+0

请参阅:http://stackoverflow.com/questions/728360/copying-an-object-in-javascript – 2012-04-27 20:03:47

回答

2

你并没有进行扩展或任何类型的继承。

这更接近:

var Person = function() { 
    this["first-name"] = 'FirstName', 
    this["last-name"] = 'LastName', 
    this["gender"] = 'Male' 
}; 

var person = new Person(); 
var anotherPerson = new Person(); 

现在你有Person 2个单独实例。如果你也想anotherPerson是一个子类..

var Person = function() { 
    this["first-name"] = 'FirstName', 
    this["last-name"] = 'LastName', 
    this["gender"] = 'Male' 
}; 

var AnotherPerson = function() { 
    this.desig = "Designation"; 
} 
AnotherPerson.prototype = new Person(); // inherit from Person 
AnotherPerson.prototype.constructor = AnotherPerson; // reset constructor 

var person = new Person(); 
var anotherPerson = new AnotherPerson(); 

console.log(person.desig); // undefined 
console.log(anotherPerson.desig); // Designation 
+1

我刚写完第一段代码; +1 – bhamlin 2012-04-27 20:05:26

+0

@Frits感谢您的回复。我得到你已经解释的代码。所以当我们做'新的对象(人)'时,'anotherPerson'指的是同一个位置?我尝试给'person'添加一个新属性,'anotherPerson'能够成功读取新属性。为什么这样? – Anji 2012-04-27 20:08:07

+0

我不完全确定'新对象(人)'是做什么的。也许它只是解决了'人'?我知道它不会从'person'神奇地延伸:P – Halcyon 2012-04-27 20:09:19

0

不是一个真正的解决方案,但对我来说,这就是我这样,我可以扩展它克隆对象:

var anotherPerson = new Object(); 
for(i in person) anotherPerson[i]=person[i]; 

而不是

var anotherPerson = new Object(person); 

然后它按预期工作。

+0

我可以知道当我们执行'var anotherPerson = new Object(person)'时会发生什么吗? – Anji 2012-04-27 20:05:08

0

Object功能(注:同样的,更令人吃惊的,如果你不熟悉,这是怎么回事,适用于呼叫new Object(...))不会创建一个新的对象,如果它传递了非布尔,数字或字符串类型的东西;它只是返回已经存在的对象。 (理由:因为它已经是一个对象,几乎一切都在Javascript对象。)

因此,anotherPerson是完全相同的对象为person,所以当你修改它,你修改person了。

我不认为这是明智的行为;但它是语言规范定义的。

0

如果你想扩展/继承一个对象,那么你可以做

var person = { 
    'first-name': 'FirstName', 
    'last-name': 'LastName', 
    'gender': 'Male' 
}; 
if (typeof Object.create !== 'function') 
{ 
    Object.create=function(o) 
    { 
     function F(){} 
     F.prototype=o; 
     return new F(); 
    } 
} 
var newPerson=Object.create(person); 
newPerson.age=25; 
newPerson.gender="Female"; 

console.log(person.gender); // Male 
console.log(newPerson.gender); // Female 

console.log(person.age); // undefined 
console.log(newPerson.age); // 25 

Fiddle

参考:Prototypal Inheritance in JavaScript

+0

道格拉斯克罗克福德的方式!完善。对, – Anji 2012-04-27 20:37:31

+0

是的,没错。 – 2012-04-27 20:38:28

0

在JavaScript

继承实现一个通用的解决方案
function inherits(base, extension) 
{ 
    for (var property in base) 
    { 
     try 
     { 
     extension[property] = base[property]; 
     } 
     catch(warning){} 
    } 
} 
+0

为什么在try/catch? – alex 2015-12-15 20:05:06