Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的继承:hasOwnProperty()_Javascript_Inheritance - Fatal编程技术网

Javascript中的继承:hasOwnProperty()

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

我在研究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 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
的正确方法。)

您可以采用不同的方式,这样就有一个对象用于
人员
部分,另一个对象继承用于
员工
部分,但是如果您想继续这样做,您可能希望停止使用构造函数(您使用的是
新建
的构造函数),而是使用生成器函数,因为