admin管理员组

文章数量:1315053

If I issue

console.dir(jQuery.prototype)

I get a beautiful list of the methods and properties that are in the jQuery object. But constructor and init are in red with a little plus sign next to them.

Q: What makes constructor and init different than the other functions?

If I issue

console.dir(jQuery.prototype)

I get a beautiful list of the methods and properties that are in the jQuery object. But constructor and init are in red with a little plus sign next to them.

Q: What makes constructor and init different than the other functions?

Share Improve this question edited Jun 23, 2011 at 11:28 Dogbert 222k42 gold badges417 silver badges415 bronze badges asked Jun 22, 2011 at 22:16 Phillip SennPhillip Senn 47.7k91 gold badges261 silver badges378 bronze badges 3
  • They look the same as everything else here on Chromium. – Matti Virkkunen Commented Jun 22, 2011 at 22:18
  • While I don't know anything for sure about this, I am willing to bet it has to do with being able to "override" methods that are attached to the jQuery object. jQuery extension methods can be made like such Object.prototype.thisFunc = function() { //dosomething }; Looking forward to the correct answer tho – MoarCodePlz Commented Jun 22, 2011 at 22:19
  • In which browser does it look different? It's all the same in Chrome. – Felix Kling Commented Jun 22, 2011 at 23:21
Add a ment  | 

3 Answers 3

Reset to default 3

Firebug checks if a function looks like a Class function (obj.prototype contains atleast 1 property), and shows it as a Class with expandable properties.

http://code.google./p/fbug/source/browse/branches/firebug1.8/content/firebug/dom/domPanel.js#531

 if (isClassFunction(val))
    this.addMember(object, "userClass", userClasses, name, val, level, 0, context);

http://code.google./p/fbug/source/browse/branches/firebug1.8/content/firebug/dom/domPanel.js#1960

function isClassFunction(fn)
{
    try
    {
        for (var name in fn.prototype)
            return true;
    } catch (exc) {}
    return false;
}

You can test it out by running this in Firebug

function isClassFunction(fn)
{
    try
    {
        for (var name in fn.prototype)
            return true;
    } catch (exc) {}
    return false;
}
test = [jQuery.prototype.init, jQuery.prototype.constructor, jQuery.prototype.each, jQuery.prototype.get];
for(var i = 0; i < test.length; i++) {
    console.log("" + i + ": " + isClassFunction(test[i]));
}

Output

0: true
1: true
2: false
3: false

I guess it's because constructor and init are not just "pure" functions. This means they have additional properties (e.g. init has its own prototype), and that's why they are expandable. To illustrate this a bit further:

// size is defined as something like this
jQuery.prototype.size = function() {
    // do stuff
};
// init is defined as a function too, but with additional properties
jQuery.prototype.init = function() {
    // do other stuff
};
jQuery.prototype.init.functionIsAnObject = true;

In other words: A function is an Object, this means you can attach any properties you want.

It shows that these functions have additional properties/methods defined for / set on them.

本文标签: javascriptjQuery constructor and initStack Overflow