我在学习代码学习Javascript。我发现这个代码:javascript中的对象的函数
// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
}
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);
// get the difference in age between alice and billy using our function
var diff = ageDifference(alice,billy);
它的工作原理和返回的差异。然而,我想知道为什么Person.alice和Person.billy输入ageDifference返回一个错误。年龄不是以Person.billy.age和Person.alice.age存储的吗?
'爱丽丝'和'比利'是对象名称,他们没有任何关系的内部结构的对象。因此'Person.billy'和'Person.alice'引用了Person类的非现有字段(即JavaScript中的原型)。 –