object. create返回的对象类型
Type of object returned by object.Create
我正在尝试掌握Javascript的某些概念。这是一个小情况
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype)
现在如果我输入
Rectangle.prototype instanceof Shape
true
现在,这是否意味着 Object.create
返回的对象的类型是Shape
?似乎是的。虽然我没有在任何地方要求它,但我只是指定了该对象的原型,应该与Shape.prototype
相同。
我只是在Object.create
文档中找不到这个文档。有人能解释一下,如果这是这种情况和(也许也是)为什么是这种情况吗?
额外的:此外,似乎通过使用行
Rectangle.prototype = Object.create(Shape.prototype)
我正在继承方法,而不是在Shape
构造函数中声明的实例变量?因为原型不知道任何关于属性内指定的Shape
构造器?例:x
、y
这是否意味着
Object.create
(在我的第一个代码片段的最后一行)返回的对象类型是Shape
?
。没有Shape
类型。ECMAScript只有以下类型:Undefined, Null, Boolean, Number, String, Symbol, Object。
Object.create
返回的值属于Object类型。
我刚刚指定了该对象的原型,它应该与
Shape.prototype
相同。
是的,这就是你得到的。Object.create
返回一个新的不可调用的普通对象,[[Prototype]]是参数。
默认情况下,instanceof
操作符只检查原型链,即属性继承。它不能保证假定的实例确实是由构造函数构造的。
window.addEventListener("DOMContentLoaded", function(){
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
outputArea.innerHTML += '<br>Shape moved.';
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype)
var outputArea = document.getElementById("output");
// Shape doesn't inherit from Shape:
outputArea.innerHTML += "<br>Shape instance of Shape: " + (Shape instanceof Shape);
// Shape is a constructor function and functions inherit from Object:
outputArea.innerHTML += "<br>Shape instance of Object: " + (Shape instanceof Object);
outputArea.innerHTML += "<br>Shape.prototype is: " + typeof Shape.prototype;
// Rectangle isn't an instance - it's a constructor function:
outputArea.innerHTML += "<br>Rectangle instance of Shape: " + (Rectangle instanceof Shape);
// But, Rectangle's prototype is an actual instance of Shape:
outputArea.innerHTML += "<br>Rectangle.prototype instance of Shape: " + (Rectangle.prototype instanceof Shape);
// But, if we make an instance of Rectangle...
var r = new Rectangle();
// Shape's constructor will be called (executed) with the new Rectangle object instance serving as the "this" object.
// r IS an instance of Rectangle which IS an instance of Shape:
outputArea.innerHTML += "<br>r instance of Shape: " + (r instanceof Shape);
// Use the inherited methods that use the instance fields:
r.move(10,20);
outputArea.innerHTML += "<br>r.x = " + r.x;
outputArea.innerHTML += "<br>r.y = " + r.y;
});
<div id="output"></div>
当你写:
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype)
你要求Rectangle
继承的对象是一个与Shape
的原型类型相同的新对象。因此,Rectangle继承了Shape为其定义的所有内容,因此Rectangle
是Shape
的实例。
Object.create(obj)
返回一个对象的新实例,该实例的原型设置为传递给create()
方法的对象的类型。这就是继承是如何建立的,如果一个对象继承了另一个对象,则可以说该对象是另一个对象的实例。
- 字符串对象类型或基元类型也是如此
- 如何在dojo中查询对象类型
- 对象类型scrollTo
- 在 Javascript 中创建全局变量时是否需要指定对象类型
- JavaScript:创建新对象类型的方法
- 如何获取对象类型
- FormData()只发送对象类型的多文件
- 获取对象类型
- 依赖对象类型检查Javascript似乎是多余的
- 访问javascript中对象类型的数组成员
- 比较对象类型时是否需要第三个=
- 如何在 Google 闭包编译器中指定对象类型的@param @return
- 窗口对象通过对象类型测试,但 hasOwnProperty 导致错误
- Javascript 空对象类型
- 何时有时将对象类型定义为将实例绑定方法镜像为构造函数上的静态实用程序函数很有用
- 在下拉列表中使用多个对象类型为数据网格提供筛选
- 为什么 String.prototype 中的“this”指的是对象类型,而不是字符串类型
- 自定义对象/类型 Javascript
- 在 Javascript 中获取对象类型的名称
- 两种 JavaScript 对象类型之间的差异