javascript riddle: 2 objects that seem identical with respect to constructor, prototype and __proto__ link, behave differently

前端 未结 1 853
野趣味
野趣味 2021-01-27 16:55

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 fa         


        
相关标签:
1条回答
  • 2021-01-27 17:40

    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.

    0 讨论(0)
提交回复
热议问题