javascript谜语:2个与构造函数,原型和__proto__链接相似的对象,表现不同 [英] javascript riddle: 2 objects that seem identical with respect to constructor, prototype and __proto__ link, behave differently

查看:109
本文介绍了javascript谜语:2个与构造函数,原型和__proto__链接相似的对象,表现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的面向对象程序员,但这让我受益匪浅!为什么我能够做新的f()而不是新的a()。我会感激任何指针。

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

我做了我的最好也提供输出。注意f和a在原型,构造函数和原型链接方面是相同的。当我尝试在最后一行中新建a()时,为什么会出现ERROR?

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?

推荐答案

正如错误所指出的那样, a 预计是一个功能,也就是说,它必须是可调用的。 new 关键字需要一个知道如何构造实例的函数对象 - 但 a 不需要。让它继承自 Function.prototype (通过使用 __ proto __ )没有任何帮助,callability是对象的固有属性。

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.

您可以拨打新f(),因为 f 这样的构造函数,由 函数构造函数

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

这篇关于javascript谜语:2个与构造函数,原型和__proto__链接相似的对象,表现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆