Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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/1/visual-studio-2012/2.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 - Fatal编程技术网

Javascript 对象。从对象创建

Javascript 对象。从对象创建,javascript,Javascript,总之,如果我将一个对象传递到对象.create,这意味着创建一个新对象继承自它。下面的代码证明了这一点 function Shape() { this.x = 0; this.y = 0; } Shape.prototype.move = function(x, y) { this.x += x; this.y += y; consol

总之,如果我将一个对象传递到
对象.create
,这意味着创建一个新对象继承自它。下面的代码证明了这一点

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

        Shape.prototype.move = function(x, y) {
            this.x += x;
            this.y += y;
            console.info("Shape moved.");
        };

        Rectangle = Object.create(Shape);
        Rectangle.__proto__==Shape;//it is true.yes, I can understand
        Rectangle//It is Function {} I can not understand it.
        Rectangle.constructor==Function//it is true.I can not understand it.
下图显示了这种关系。但我不能理解的是它的突出部分。
矩形到底是什么?我的意思是什么是
函数{}
,它来自哪里?还有
Rectangle.constructor
属性,我不知道所有对象是否都有
constructor
属性,以及
constructor
属性的用途是什么?谢谢

PS:以上所有数值都是在FireBug中计算和观察的

根据minitech的评论更正图表


这就是使用
Object.create进行继承的方式。它应该是这样的:

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

Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

function Rectangle() {
    Shape.call(this);
}

Rectangle.prototype = Object.create(Shape.prototype); // Leaving out the constructor business for simplicity
你在那里做的是复制实际的
形状
函数,所以当然(作为一个函数)它的构造函数是
函数


请注意,矩形。uuu proto_uuu=Shape
不是一个比较。

+1-+2当您添加一些有关构造函数及其功能的信息时;-)@西恩维拉:哪个构造器<代码>函数
矩形
或“排序
矩形
”?:DHi,所有人,很抱歉错过了另一个
=
,应该是“==”,请再次查看。谢谢。它应该是
矩形。\uuuuu proto\uuuuu==Shape
嗨,所有,在我的测试代码中,我只想创建从
Shape
继承的对象,而不是
Shape.prototype
,并尝试弄清楚如果这样做会发生什么。谢谢