开发者

Firebug constructor function vs user function

开发者 https://www.devze.com 2023-01-21 00:31 出处:网络
I made a quick search about what is the meaning of Firebug DOM tab coloring and 开发者_运维百科I see the the explanation below from here

I made a quick search about what is the meaning of Firebug DOM tab coloring and 开发者_运维百科I see the the explanation below from here

Red colored bold text points "constructor function"

Green colored bold text points "user function".

What is the difference between the two type of functions?


The jQuery function is a constructor function ($ is merely a reference to jQuery). You can see that when you see its definition:

var jQuery = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context );
},
…
jQuery.fn = jQuery.prototype = {
    init: function( …

You can reproduce this behaviour (the bold red coloring) when defining a constructor function and adding something to its prototype object, like

var testFunc = function () {
    /* nothing so far */
};

testFunc.prototype.baz = function () {
    /* nothing, this gets boring */
};

or even just a random number

testFunc.prototype.baz = 4;

Note that this doesn't comply with the actual definition of a constructor function in JavaScript. If you test the linked source code in Firebug, car will be colored green, not red. Furthermore, see The Benefits of JavaScript Prototype.


As Marcel's example shows, Firebug marks objects that typeof "function" and which have a property 'prototype' with at least one subproperty as a 'userClass'

http://code.google.com/p/fbug/source/browse/branches/firebug1.7/content/firebug/dom.js#431

There really isn't any such thing as a "constructor function" in Javascript, just functions that can be used as constructors. Any function can be used to create objects, but it's only really interesting if the function has a prototype.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号