Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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 为什么我们不能调用原型函数?_Javascript_Class_Inheritance_Prototypal Inheritance - Fatal编程技术网

Javascript 为什么我们不能调用原型函数?

Javascript 为什么我们不能调用原型函数?,javascript,class,inheritance,prototypal-inheritance,Javascript,Class,Inheritance,Prototypal Inheritance,最初,我将函数对象分配给变量Person。此时,Person的proto指向Function.prototype。然后我向Person.prototype添加了一些函数。当我用下面的new关键字调用Person构造函数并将其分配给var test时,据我所知,它将testproto设置为Person.prototype。This Person.prototype是一个对象,getName映射到一个函数。因此,当我调用test.getName()时,它会在test.prototype中搜索函数,如

最初,我将函数对象分配给变量Person。此时,Person的proto指向Function.prototype。然后我向Person.prototype添加了一些函数。当我用下面的new关键字调用Person构造函数并将其分配给var test时,据我所知,它将testproto设置为Person.prototype。This Person.prototype是一个对象,getName映射到一个函数。因此,当我调用test.getName()时,它会在test.prototype中搜索函数,如果在那里找不到,它会在其proto中搜索函数,即Person.prototype

现在假设我创建另一个函数对象并将其分配给变量Customer。它是proto,指向Function.prototype。那么对于继承,我们应该执行Customer.prototype=newperson()。我不明白我为什么要这样做?它是否将客户的原型设置为Person.prototype。如果是这样,那么我不应该只写Customer=newperson()。如果没有,客户proto是否仍然指向Function.prototype,或者我是否还缺少其他东西

var Person = function(name) {
    this.name = name;
    console.log("Running the constructor of Person " + name)
}

Person.prototype.getName = function() {
    return this.name;
}

var test = new Person("Yolo");
console.log(Person.prototype.getName.call(test))
console.log(test.getName());

var Customer = function(name) {
    this.name = name;
    console.log("Running the constructor of Customer " + name)
};

Customer.prototype = new Person();
提前谢谢

Customer.___proto____ = Function.prototype 
它将始终指向函数原型

Customer.prototype = new Person();
此语句将使Customer.prototype.________;指向Person.prototype

客户。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu


我发现这张图片很有用。它清楚地显示了所有链接。

这里有两个不同的概念:

  • \uuuu proto\uuu
    是继承树中对象的父对象
  • prototype
    是一个函数的属性,它定义了当作为构造函数使用时所创建的任何对象的
    \uuuuuuuuuuuuuuuuuuuuuuuu
    的内容
由于构造函数本身就是对象(函数对象),它们也位于继承树中,但该树与这些构造函数创建的对象的继承树无关。它们是两个不同的世界

请看这段代码,它演示了客户对象的继承链:

var cust = new Customer('Jack');
console.log(cust.__proto__ === Customer.prototype); // true
console.log(cust.__proto__.__proto__ === Person.prototype); // true
console.log(cust.__proto__.__proto__.__proto__ === Object.prototype); // true
因此,这里我们遵循了
cust
对象的原型链。这是一个与构造函数(函数)链完全不同的链:

这里没有什么不寻常的:它们是函数。此外,没有必要对此进行任何更改。如上所述,它与这些构造函数创建的对象的继承无关


请注意,您不应该需要访问
\uuuu proto\uuu
属性。上述内容仅用于说明继承链。

提示:
var test=new Person()
var test.prototype=new Person()
这是一个实现细节,不属于javascript标准的一部分。始终使用
.prototype
这就是继承的工作原理。我只是解释如何设置proto链接。显然,这些proto链接在code中没有使用,但当我这样做时,var test2=new Customer();并调用test2.getName(),然后它将在test2.prototype中搜索函数,然后在test2 proto prototype中,即Customer.prototype,它将在那里找不到它,因此它将在Customer的proto中,即function.prototype中搜索它。所以,这样它就永远无法达到真正的功能。最后是启蒙时刻。谢谢。
console.log(Customer.__proto__ === Function.prototype); // true
console.log(Person.__proto__ === Function.prototype); // true
console.log(Object.__proto__ === Function.prototype); // true