不工作的Object.create(Error.prototype)

时间:2016-10-22 16:47:42

标签: javascript jquery prototype frontend

我有代码:

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)?

1 个答案:

答案 0 :(得分:3)

问题不在于Object.create。问题是你从构造函数中return that;

thatError的实例,而不是TestError的实例。因此,当您正确地why添加到TestError.prototype时,只有TestError的实例会使用该方法,而不是Error

这大致是第一个例子中的环境状态:

+--------------------+       +------------------------+        +-----------------+
|     TestError      |       |  TestError.prototype   | +----->| Error.prototype |
+------------+-------+       +-------------+----------+ |      +-----------------+
|  prototype |   *---+------>|     why     |<Function>| |               ^         
+------------+-------+       +-------------+----------+ |               |         
                             |[[Prototype]]|      *---+-+               |         
                             +-------------+----------+                 |         
                                                                        |         
                                                                        |         
                             +------------------------+                 |         
+--------------------+       |        <Object>        |                 |         
|        err         |------>+-------------+----------+                 |         
+--------------------+       |[[Prototype]]|     *----+-----------------+         
                             +-------------+----------+                           

删除行return that;,它会起作用。

&#13;
&#13;
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;
&#13;
&#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]]|     *----+---+            
                             +-------------+----------+                

&#13;
&#13;
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;
&#13;
&#13;