gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:12:45 25 4
gpt4 key购买 nike

我是一位经验丰富的面向对象程序员,但这让我着迷!为什么我可以执行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com