Javascript中的继承:hasOwnProperty()
我在研究javascript中的继承概念。 考虑下面的片段:Javascript中的继承:hasOwnProperty(),javascript,inheritance,Javascript,Inheritance,我在研究javascript中的继承概念。 考虑下面的片段: var Person=function(firstname,lastname,gender){ this.firstname=firstname; this.lastname=lastname; this.gender=gender; this.name=function(){ return this.firstname +" "+this.lastname; } } var Empl
var Person=function(firstname,lastname,gender){
this.firstname=firstname;
this.lastname=lastname;
this.gender=gender;
this.name=function(){
return this.firstname +" "+this.lastname;
}
}
var Employee = function(firstName, lastName, gender, title) {
Person.call(this, firstName, lastName, gender);
this.title = title;
};
var e = new Employee('Abc', 'efg', 'Tree', 'CEO');
现在,如果我们检查以下内容:
console.log(e.hasOwnProperty('firstName') ) ==> TRUE
我想知道有没有什么方法可以让我们完全继承父母的价值观?
以致
console.log(e.hasOwnProperty('firstName') ) ==> False
这是完全正确的,您通过调用父构造函数
Person.call(This,…)
继承了Person的属性,并将Employee
实例的This
传递给Person
构造函数
当您调用call
并传递Employee
实例的this
上下文时,它将实际使用Employee
的this
来执行这些分配操作:
this.firstname = firstname;
this.lastname = lastname;
this.gender = gender;
this.name = function(){
return this.firstname +" "+this.lastname;
}
此处此
引用新的员工
实例。由于上述分配使这些属性在新的Employee
实例上显示为own properties,hasOwnProperty
方法使用这些属性时,返回true
这是在JavaScript中执行数据属性继承的正确方法。对于方法,我们应该将它们放在原型中
这是因为数据属性不应在员工的不同实例之间共享,但方法可以共享,因为它们暗示了常见的行为
如果不将方法放在原型中
并将它们作为自己的属性,这将成为一种开销,因为存在相同的逻辑,但方法对于构造函数的每个实例都是不同的实例
为了完成您的示例,hasOwnProperty
返回name
方法的false
,与prototype
相同,而其他数据属性将返回true
:
var Person=function(名字、姓氏、性别){
this.firstname=firstname;
this.lastname=lastname;
这个。性别=性别;
}
Person.prototype.name=函数(){
返回this.firstname+“”+this.lastname;
}
var Employee=职能部门(名、姓、性别、职务){
Person.call(这个,名字,姓氏,性别);
this.title=标题;
};
Employee.prototype=Object.create(Person.prototype);
Employee.prototype.constructor=员工;
var e=新员工(“Abc”、“efg”、“Tree”、“CEO”);
//打印正确
log(例如hasOwnProperty('firstname');
//打印错误
log(例如hasOwnProperty('name'))
这完全没问题,您通过调用父构造函数Person.call(This,…)
继承了Person的属性,并将Employee
实例的This
传递给Person
构造函数
当您调用call
并传递Employee
实例的this
上下文时,它将实际使用Employee
的this
来执行这些分配操作:
this.firstname = firstname;
this.lastname = lastname;
this.gender = gender;
this.name = function(){
return this.firstname +" "+this.lastname;
}
此处此
引用新的员工
实例。由于上述分配使这些属性在新的Employee
实例上显示为own properties,hasOwnProperty
方法使用这些属性时,返回true
这是在JavaScript中执行数据属性继承的正确方法。对于方法,我们应该将它们放在原型中
这是因为数据属性不应在员工的不同实例之间共享,但方法可以共享,因为它们暗示了常见的行为
如果不将方法放在原型中
并将它们作为自己的属性,这将成为一种开销,因为存在相同的逻辑,但方法对于构造函数的每个实例都是不同的实例
为了完成您的示例,hasOwnProperty
返回name
方法的false
,与prototype
相同,而其他数据属性将返回true
:
var Person=function(名字、姓氏、性别){
this.firstname=firstname;
this.lastname=lastname;
这个。性别=性别;
}
Person.prototype.name=函数(){
返回this.firstname+“”+this.lastname;
}
var Employee=职能部门(名、姓、性别、职务){
Person.call(这个,名字,姓氏,性别);
this.title=标题;
};
Employee.prototype=Object.create(Person.prototype);
Employee.prototype.constructor=员工;
var e=新员工(“Abc”、“efg”、“Tree”、“CEO”);
//打印正确
log(例如hasOwnProperty('firstname');
//打印错误
log(例如hasOwnProperty('name'))
这里需要了解的一点很重要,新员工只创建了一个对象。该对象具有由Person
和Employee
为其定义的特征组合,但它是一个对象。所有this.x=y构造函数中的代码>语句创建该对象自己的属性。事实上,这个特定的例子没有定义任何有用的东西供它继承,因为没有任何东西放在原型链中的对象上。(原型链的形式也有点不正确,请参阅,以了解在ES5和ES2015+中连接Employee
和Person
的正确方法。)
您可以采用不同的方式,这样就有一个对象用于人员
部分,另一个对象继承用于员工
部分,但是如果您想继续这样做,您可能希望停止使用构造函数(您使用的是新建
的构造函数),而是使用生成器函数,因为