Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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_Function_Inheritance_Constructor_Prototype - Fatal编程技术网

JavaScript惰性。在所示的示例代码中它是如何工作的?

JavaScript惰性。在所示的示例代码中它是如何工作的?,javascript,function,inheritance,constructor,prototype,Javascript,Function,Inheritance,Constructor,Prototype,下面的链接有我在这里展示的示例代码,注释是我的 代码片段: function Point(x, y) { this.x = x; this.y = y; } function ColorPoint(x, y, color) { Point.call(this, x, y); this.color = color; } function inherits(SubC, SuperC) { var subProto = Object.create(Super

下面的链接有我在这里展示的示例代码,注释是我的

代码片段:

function Point(x, y) {
    this.x = x;
    this.y = y;
}

function ColorPoint(x, y, color) {
    Point.call(this, x, y);
    this.color = color;
}

function inherits(SubC, SuperC) {
    var subProto = Object.create(SuperC.prototype);
    // At the very least, we keep the "constructor" property
    // At most, we preserve additions that have already been made
    extend(subProto, SubC.prototype);
    SubC.prototype = subProto;
    SubC._super = SuperC.prototype;
}

function extend(target, source) {
    Object.getOwnPropertyNames(source)
        .forEach(function(property) {
            Object.defineProperty(target, property,
                Object.getOwnPropertyDescriptor(source, property));
        });
    return target;
}

Point.prototype.toString = function() {
    return "(" + this.x + "," + this.y + ")";
};

ColorPoint.prototype.toString = function() {
    return this.color + " " + Point.prototype.toString.call(this);
};

console.log(Point);
console.log(ColorPoint);

inherits(ColorPoint, Point);

console.log(ColorPoint.prototype); //Refers to the object from which all the instances are created from

console.log(ColorPoint.prototype.constructor); //The function itself

var greenPoint = new ColorPoint(1, 2, "Green");

console.log(greenPoint);

console.log(greenPoint.prototype); //Undefined. Objects have no prototype property

console.log(greenPoint.constructor); // greenPoint as an object has constructor, from which it is created i.e. function ColorPoint

console.log(greenPoint.constructor.prototype); //The constructor ColorPoint
我无法理解inherits方法在这里所做的事情。我非常了解它是如何使用下面的策略工作的

ColorPoint.prototype = Object.create(Point.prototype);//Point prototype is referenced to ColorPoints'
ColorPoint.prototype.constructor = ColorPoint;//Reset constructor

但我不明白subto.constructor为什么指的是ColorPoint。接下来,为什么扩展方法参数不分别像ColorPoint、Point child和parent那样传递。

当您使用新的ColorPoint从构造函数函数创建实例时,ColorPoint.prototype.Constructor属性指向构造函数->ColorPoint

当您使用ColorPoint.prototype=Object.create时,您可以使用Point构造函数重写构造函数,这只是将其设置回正确的函数

在extend方法中,源对象的所有属性都将传递给目标对象