简体   繁体   English

为什么在JavaScript中将object和Constructor.prototype设置为Base?

[英]Why are the object and constructor.prototype set to Base in JavaScript?

Here is my little program. 这是我的小程序。 When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...}. 当我在调试模式下检查rec的值时,该对象为Base {x = 0,y = 0,w = 10,more ...}。 Should it be Rectangle? 应该是矩形吗? Also the constructor.prototype is Base. 而且builder.prototype也是Base。 Why not Shape? 为什么不整形?

    function Base() {
    }

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

    Shape.prototype = new Base();
    Shape.prototype.move = function(x, y) {
        this.x += x;
        this.y += y;
        console.log("x = " + this.x + " y = " + this.y);
    };

    function Rectangle(x, y, w, h) {
        Shape.call(this, x, y);
        this.w = w;
        this.h = h;
    }

    Rectangle.prototype = new Shape();
    Rectangle.prototype.area = function() {
        return this.w * this.h;
    };
    var rec = new Rectangle(0, 0, 10, 10);
    console.log(instanceOf(rec, Rectangle));

    function instanceOf(object, constructor) { 
        while (object != null) {
            if (object == constructor.prototype)
                return true;
            if ( typeof object == 'xml') {
                return constructor.prototype == XML.prototype;
            }
            object = object.__proto__;
        }
        return false;
    }

Have a look at Why [not] to use the new keyword here? 看看为什么[不]以使用new这里的关键字? . You might not use it and create a new instance of it, but rather just inherit from Base.prototype . 您可能不使用它并创建它的新实例,而只是继承自Base.prototype

Also the constructor.prototype is Base. 而且builder.prototype也是Base。 Why not Shape? 为什么不整形?

I'm not sure which constructor you are referring to here: 我不确定在这里指的是哪个constructor

  • The constructor property of all your objects is Base , as all of them inherit this prototype from the Base.prototype object. 您所有对象的constructor属性都是Base ,因为它们都从Base.prototype对象继承了此原型。 You did not overwrite it after setting up the inheritance chains. 建立产业链后没有覆盖它。 It is not really necessary, but good style: Shape.prototype.constructor = Shape and Rectangle.prototype.constructor = Rectangle - where those prototype objects are the overwritten ones which inherit from Base . 确实不是必需的,而是好的样式: Shape.prototype.constructor = ShapeRectangle.prototype.constructor = Rectangle这些原型对象是从Base继承的覆盖对象。

  • The constructor parameter of your instanceOf function. 您的instanceOf函数的constructor参数。 You pass in Rectangle there, so constructor.prototype is the prototype object of Rectangle , which inherits from Base but is different. 您在此处传递Rectangle ,所以constructor.prototypeRectangle的原型对象,该对象继承Base但有所不同。

When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...} 当我在调试模式下检查rec的值时,该对象为Base {x = 0,y = 0,w = 10,more ...}

Usually not. 通常不会。 Is Base something special, eg a host object? Base是否有特殊之处,例如宿主对象? Your rec object is an instance of Base , so it might be displayed differently because of that. 您的rec对象是Base一个实例,因此可能因此显示不同。

rec is just an object which inherits from Rectangle.prototype which inherits from Shape.prototype which inherits from Base.prototype which inherits from​… Assuming Base is the function you defined, from Object.prototype which inherits from null . rec只是一个继承自Rectangle.prototype的对象,而Rectangle.prototype继承自Shape.prototype ,而Shape.prototype继承自Base.prototype ,后者继承自...。假设Base是您定义的函数,而Object.prototype继承自null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript继承constructor.prototype - JavaScript Inheritance constructor.prototype 为什么我必须在OOP Javascript中使用constructor.prototype? - Why do I have to use the constructor.prototype in OOP Javascript? 为什么Object.getPrototypeOf()和Constructor.prototype记录不同的值 - Why Object.getPrototypeOf() and constructor.prototype logs different values Constructor.prototype不在原型链中? - Constructor.prototype not in the prototype chain? 为什么 object 实例即使在 JS 中继承也不显示其 Constructor.prototype 的属性? - Why does object instances not show the properties of their Constructor.prototype even when they are inherited in JS? 使用constructor.prototype访问超类成员的Javascript失败了吗? - Javascript using constructor.prototype to visit superclass member fails? 如何在Javascript的Constructor.prototype中定义局部函数 - How to define a local function in Constructor.prototype in Javascript 使用constructor.prototype遍历原型链 - Traversing prototype chain using constructor.prototype 如何在Internet Explorer &lt;= IE8中的DOM对象的Constructor.prototype中添加属性? - How to add a property in the DOM Object's constructor.prototype in Internet Explorer<=IE8? 实例的 [[prototype]] 插槽如何以及为什么在它们被制作的时间点包含 Constructor.prototype(并且没有得到“更新”)? - How and why do the [[prototype]] slots of instances contain the Constructor.prototype at the point in time they were made (and don't get 'updated')?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM