javascript Riddle : 2 objects that seem identical with respect to constructor, 原型(prototype)和 __proto__ 链接,行为不同

标签 javascript inheritance prototype ecmascript-5 prototypal-inheritance

我是一位经验丰富的面向对象程序员,但这让我着迷!为什么我可以执行 new f() 而不能执行 new a()。我将不胜感激任何指点。

 // first a few facts 
if (Object instanceof Function) console.log("Object isa Function");
console.log("Function.prototype is " + Function.prototype);
/* output
 Object isa Function
  Function.prototype is function Empty() {}
*/

var f = new Function();
console.log("Prototype of f:" + f.prototype);
console.log("Constructor of f:" + f.constructor);
console.log("Prototype Link of f:" + f.__proto__);
if (f instanceof Function) console.log("f isa Function");
/* output
Prototype of f:[object Object]
Constructor of f:function Function() { [native code] }
Prototype Link of f:function Empty() {}
 f isa Function
*/


function A() {}
console.log("Prototype of A:" + A.prototype);
console.log("Constructor of A:" + A.constructor);
console.log("Prototype Link of A:" + A.__proto__);
if (A instanceof Function) console.log("A isa Function");
/*
Prototype of A:[object Object]
Constructor of A:function Function() { [native code] }
Prototype Link of A:function Empty() {}
A isa Function
*/

 // contruct a
var a = new A();
console.log("Prototype of a:" + a.prototype);
console.log("Constructor of a:" + a.constructor);
console.log("Prototype Link of a:" + a.__proto__);
if (a instanceof Function) console.log("a isa Function");
if (a instanceof A) console.log("a isa A");
/* output
 Prototype of a:undefined
Constructor of a:function A(){}
Prototype Link of a:[object Object]
a isa A
*/

console.log("~~~~~b constructed as new f()");
var b = new f();
console.log("Prototype of b:" + b.prototype);
console.log("Constructor of b:" + b.constructor);
console.log("Prototype Link of b:" + b.__proto__);
/* output
 ~~~~~b constructed as new f()
Prototype of b:undefined
Constructor of b:function anonymous() {}
Prototype Link of b:[object Object]
*/

console.log("~~~~~b constructed as new a()");
a.prototype = Object.prototype;
a.constructor = Function;
a.__proto__ = Function.prototype;
if (a instanceof Function) console.log("a isa Function");
console.log("Prototype of a:" + a.prototype);
console.log("Constructor of a:" + a.constructor);
console.log("Prototype Link of a:" + a.__proto__);
/* output
~~~~~b constructed as new a()
a isa Function
Prototype of a:[object Object]
Constructor of a:function Function() { [native code] }
Prototype Link of a:function Empty() {}     
*/
b = new a();
/* ERROR  Uncaught TypeError: object is not a function*/

我也已尽力提供输出。请注意,f 和 a 在原型(prototype)、构造函数和原型(prototype)链接方面是相同的。当我尝试在最后一行中 new a() 时,为什么会出现错误?

最佳答案

正如错误指出的那样,a 应该是一个函数,即它必须是可调用的。 new 关键字需要一个知道如何构造实例的函数对象,但 a 不知道。让它继承 Function.prototype (通过使用 __proto__)没有任何帮助,可调用性是对象的固有属性。

您可以调用new f(),因为f这样的构造函数,由 Function constructor 创建.

关于javascript Riddle : 2 objects that seem identical with respect to constructor, 原型(prototype)和 __proto__ 链接,行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494663/

相关文章:

css - 元素动画关键帧的继承

jquery - this.toPlatedString 不是函数原型(prototype)

c++ - 继承期间无法访问基址

javascript - 获取 javascript 来警告用户此正则表达式测试

javascript - javascript 中 rxjs5 组合的示例

javascript - 通过 WebApi 将媒体批量上传到 Azure Blob 存储

java - 如何在不使用 setter 的情况下从子类访问父类(super class)中的私有(private)字段?

javascript - 如何防止Object.prototype被修改?

Javascript : Modifying Prototype doesn't affect existing Instances

javascript - 没有表单需要 html5 输入类型。它有效吗?