最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the default prototype for custom function in JavaScript? - Stack Overflow

programmeradmin1浏览0评论
function f()
{
}

alert (f.prototype); // returns something like [object Object]

My understanding is by default the prototype of custom function should be null or undefined, can someone shed some light? thanks!

See also: How does __proto__ differ from constructor.prototype?

function f()
{
}

alert (f.prototype); // returns something like [object Object]

My understanding is by default the prototype of custom function should be null or undefined, can someone shed some light? thanks!

See also: How does __proto__ differ from constructor.prototype?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 26, 2010 at 20:10 nandinnandin 2,5755 gold badges24 silver badges28 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 11

The prototype property of function objects is automatically created, is simply an empty object with the {DontEnum} and {DontDelete} property attributes, you can see how function objects are created in the specification:

  • 13.2 Creating Function Objects

Pay attention to the steps 9, 10 and 11:

9) Create a new object as would be constructed by the expression new Object().

10) Set the constructor property of Result(9) to F. This property is given attributes { DontEnum }.

11) Set the prototype property of F to Result(9). This property is given attributes as specified in 15.3.5.2.

You can see that this is true by:

function f(){
  //...
}

f.hasOwnProperty('prototype'); // true, property exist on f

f.propertyIsEnumerable('prototype'); // false, because the { DontEnum } attribute

delete f.prototype; // false, because the { DontDelete } attribute

Here is a link describing object inheritance:

http://javascript.crockford.com/prototypal.html

http://www.mollypages.org/misc/js.mp
(source: mollypages.org)

It's not undefined because you just defined it. Just because your function f() object is still empty doesn't mean it's not defined. It's just defined to have no contents.

发布评论

评论列表(0)

  1. 暂无评论