javascript riddle: 2 objects that seem identical w

2020-05-06 08:38发布

I am an experienced object oriented programmer but this got me! Why am I able to do new f() but not new a(). I will appreciate any pointers.

 // 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*/

I have done my best to provide the output as well. notice that f and a are identical in terms of prototype, constructor and prototype link. Why does the ERROR appear when I try to new a() in the last line?

1条回答
神经病院院长
2楼-- · 2020-05-06 08:41

As the error points out, a is expected to be a function, that is, it must be callable. The new keyword requires a function object that knows how to construct an instance - but a does not. Letting it inherit from Function.prototype (by using __proto__) does not help anything, callability is an intrinsic property of objects.

You are able to call new f(), as f is such a constructor function, being created by the Function constructor.

查看更多
登录 后发表回答