Javascript 调用基础 class 构造函数
Javascript call the base class constructor
Person
是基础 class 并且 Emp
继承自 Person
。我正在尝试在 Emp
中使用 Person
的 name
、location
属性。
function Person(name, location){
this.name = name;
this.location = location;
}
Person.prototype.getName = function(){
return 'Name: ' + this.name;
}
function Emp(id, name, location){
this.id = id;
Person.call(this, name);
Person.call(this, location);
}
Emp.prototype = Object.create(Person.prototype);
var e1 = new Emp(1, 'John', 'London');
e1.id // 1
e1.name // 'London'
e1.location //undefined
导致此错误的原因是什么?为什么 name 取 London 的值?
为什么用一个参数调用构造函数两次?
function Emp(id, name, location){
this.id = id;
Person.call(this, name, location);
}
Person
是基础 class 并且 Emp
继承自 Person
。我正在尝试在 Emp
中使用 Person
的 name
、location
属性。
function Person(name, location){
this.name = name;
this.location = location;
}
Person.prototype.getName = function(){
return 'Name: ' + this.name;
}
function Emp(id, name, location){
this.id = id;
Person.call(this, name);
Person.call(this, location);
}
Emp.prototype = Object.create(Person.prototype);
var e1 = new Emp(1, 'John', 'London');
e1.id // 1
e1.name // 'London'
e1.location //undefined
导致此错误的原因是什么?为什么 name 取 London 的值?
为什么用一个参数调用构造函数两次?
function Emp(id, name, location){
this.id = id;
Person.call(this, name, location);
}