Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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 .isPrototypeOf()和.hasOwnProperty()方法混淆_Javascript - Fatal编程技术网

Javascript .isPrototypeOf()和.hasOwnProperty()方法混淆

Javascript .isPrototypeOf()和.hasOwnProperty()方法混淆,javascript,Javascript,假设我有这个代码: //男性将继承所有人类财产 功能人(x,y){ //将继承以下属性 this.name=x; 这个年龄=y; this.test=“test 1”; } //以下属性也将被继承 Human.prototype.citizen=“美国”; Human.prototype.employer=“谷歌”; Human.prototype.test=“测试2”; 功能外螺纹(x,y){ //以下属性将是男性实例的自身属性 this.name=x; 这个年龄=y; this.gende

假设我有这个代码:

//男性将继承所有人类财产
功能人(x,y){
//将继承以下属性
this.name=x;
这个年龄=y;
this.test=“test 1”;
}
//以下属性也将被继承
Human.prototype.citizen=“美国”;
Human.prototype.employer=“谷歌”;
Human.prototype.test=“测试2”;
功能外螺纹(x,y){
//以下属性将是男性实例的自身属性
this.name=x;
这个年龄=y;
this.gender=“男”;
}
//继承-连接男性对象和人类对象
Male.prototype=新人类();//不传递任何参数
Male.prototype.constructor=Male;//更正构造函数属性

var albert=新男性(“albert”,25岁)人类
功能不在阿尔伯特的原型链中。
Human.prototype
引用的对象是:

Human.prototype.isPrototypeOf(albert); // true
人类。拥有自己的财产(“年龄”);///我希望它会回到现实


Human
函数没有
age
属性。通过
new
do创建的实例:

new Human().hasOwnProperty("age"); // true

旁注:您设置继承链的方式很常见,并在许多示例中显示,但在两个方面不正确:

  • 您不想使用
    新人类
    创建
    男性原型

  • 您确实想从
    Male
    呼叫
    Human

  • 因此:

    您不使用
    新人类
    男性
    创建原型的原因很简单:
    人类
    需要参数,但您没有任何参数

    以下是该代码的更新ES5和早期版本:

    function-Human(name,age){//参数名称应该有意义
    this.name=名称;
    这个。年龄=年龄;
    this.test=“test 1”;
    }
    Human.prototype.citizen=“美国”;
    Human.prototype.employer=“谷歌”;
    Human.prototype.test=“测试2”;
    功能男性(姓名、年龄){
    人类。称呼(这个、名字、年龄);
    this.gender=“男”;
    }
    Male.prototype=Object.create(Human.prototype);
    Male.prototype.constructor=Male;
    var albert=新男性(“albert”,25岁);
    console.log(Human.prototype.isPrototypeOf(albert));//真的
    
    console.log(new Human().hasOwnProperty(“age”);//true
    每个问题可能重复一个问题,但您从未定义任何
    Human.age
    ,因此应为false(您正在
    Human
    实例上设置
    .age
    )。对于第二个问题,如果
    Human.hasOwnProperty(“age”)
    不起作用,因为它不是实例,为什么
    Male.hasOwnProperty(“name”)
    将返回
    true
    ,而
    Male.hasOwnProperty(“age”)
    将返回
    false
    ?@xcode:因为从ES2015(也称为“ES6”)开始,函数具有
    name
    属性。如果你看
    Male.name
    ,你会看到它是
    “Male”
    。同样对于note
    Human.hasOwnProperty(“name”)
    将返回
    true
    ,那么这里发生了什么?我真的很想了解这个链条是如何工作的。感谢you@xcode:同样,函数具有
    名称
    属性
    Human.name
    将是
    “Human”
    @xcode:我将添加一个快速图表,我认为它可能会有所帮助。
    function Male(x, y) {
        // Give Human its chance to initialize the object (#2)
        Human.call(this, x, y);
        // ...
    }
    
    // Don't use new Human to create the prototype (#1)
    Male.prototype = Object.create(Human.prototype);
    Male.prototype.constructor = Male;