javascript - 不工作 Object.create(Error.prototype)

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

最佳答案

问题不在于 Object.create。问题是您从构造函数中返回了;

thatError 的实例,而不是 TestError 的实例。因此,当您正确why添加到TestError.prototype时,只有TestError的实例才会具有该方法,而不是错误

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

+--------------------+       +------------------------+        +-----------------+
|     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();

因为这就是你想要的:

+--------------------+       +------------------------+        +-----------------+
|     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

关于javascript - 不工作 Object.create(Error.prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194611/

相关文章:

Javascript 'undefined is not a function' - 你能澄清这个例子吗?

jquery - 在括号中堆叠更多变量以触发 .remove()

jquery - 这个 JavaScript 单元可以使用 Jasmine 进行测试吗?

javascript - 访问原型(prototype)中的主要对象变量

javascript - 使用原型(prototype) apply vs this 来调用函数

javascript - 经典继承与原型(prototype)继承的区别

javascript - Highcharts 显示具有多个数据系列的相同 y 轴开始值和结束值

javascript - 在 IE 8 中加速 ":not"jQuery CSS 选择器?

javascript - jQuery - 输入字段中的多个电子邮件地址

javascript - 尝试制作一个自动添加数字的表单,而不像事件监听器那样提交