我有代码:
var TestError = function () {
var that = Error.call(this);
return that;
};
TestError.prototype = Object.create(Error.prototype);
TestError.prototype.why = function(){alert("NOT WORKING")}
var err = new TestError();
err.why();//not working :( TypeError: err.why is not a function
但没有工作:(
写作时
var TestError = function () {
var that = Error.call(this);
return that;
};
TestError.prototype = Error.prototype;
TestError.prototype.why = function(){alert("WORKING")}
var err = new TestError();
err.why();// working :(
为什么我不能使用Object.create(Error.prototype)?
答案 0 :(得分:3)
问题不在于Object.create
。问题是你从构造函数中return that;
。
that
是Error
的实例,而不是TestError
的实例。因此,当您正确地将why
添加到TestError.prototype
时,只有TestError
的实例会使用该方法,而不是Error
。
这大致是第一个例子中的环境状态:
+--------------------+ +------------------------+ +-----------------+
| TestError | | TestError.prototype | +----->| Error.prototype |
+------------+-------+ +-------------+----------+ | +-----------------+
| prototype | *---+------>| why |<Function>| | ^
+------------+-------+ +-------------+----------+ | |
|[[Prototype]]| *---+-+ |
+-------------+----------+ |
|
|
+------------------------+ |
+--------------------+ | <Object> | |
| err |------>+-------------+----------+ |
+--------------------+ |[[Prototype]]| *----+-----------------+
+-------------+----------+
删除行return that;
,它会起作用。
var TestError = function() {
Error.call(this);
};
TestError.prototype = Object.create(Error.prototype);
TestError.prototype.why = function() {
alert("WORKING")
}
var err = new TestError();
err.why();
&#13;
因为这是你想要的:
+--------------------+ +------------------------+ +-----------------+
| TestError | | TestError.prototype | +----->| Error.prototype |
+------------+-------+ +-------------+----------+ | +-----------------+
| prototype | *---+------>| why |<Function>| |
+------------+-------+ +-------------+----------+ |
|[[Prototype]]| *---+-+
+-------------+----------+
^
|
+---------------+
|
+------------------------+ |
+--------------------+ | <Object> | |
| err |------>+-------------+----------+ |
+--------------------+ |[[Prototype]]| *----+---+
+-------------+----------+
为什么没有Object.create
会有效?因为当你做的时候
TestError.prototype = Error.prototype;
您对TestError.prototype
的每个扩展程序都会延伸Error.prototype
。因此,当您添加why
时,Error
的每个实例都将使用该方法。
如上所述,return that;
会返回Error
的实例。由于所有错误实例现在都有一个why
方法,但这是有效的,而不是偶然的,而不是故意的。
+--------------------+
| TestError | +------------------------+
+------------+-------+ | Error.prototype |
| prototype | *---+---------------------->+-------------+----------+
+------------+-------+ | why |<Function>|
+-------------+----------+
^
|
|
+------------------------+ |
+--------------------+ | <Object> | |
| err |------>+-------------+----------+ |
+--------------------+ |[[Prototype]]| *----+---+
+-------------+----------+
var TestError = function () {
var that = Error.call(this);
return that;
};
TestError.prototype = Error.prototype;
TestError.prototype.why = function(){alert("WORKING")}
var err = new Error(); // <- Note that we execute Error here
err.why();// working, but it shouldn't. Every Error instance now has a why method
&#13;