jQuery 构造函数和初始化

发布于 2024-11-16 12:20:39 字数 165 浏览 0 评论 0原文

如果我发出问题,

console.dir(jQuery.prototype)

我会得到 jQuery 对象中的方法和属性的漂亮列表。但构造函数和 init 是红色的,旁边有一个小加号。

问:构造函数和 init 与其他函数有何不同?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所谓啦 2024-11-23 12:20:39

Firebug 检查函数是否看起来像类函数(obj.prototype 包含至少 1 个属性),并将其显示为具有可扩展属性的类。

http:// /code.google.com/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.com/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;
}

中运行此命令来测试它

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]));
}

您可以通过在 Firebug输出

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

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.com/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.com/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
愁杀 2024-11-23 12:20:39

我想这是因为构造函数和 init 不仅仅是“纯”函数。这意味着它们具有附加属性(例如 init 有其自己的原型),这就是它们可扩展的原因。为了进一步说明这一点:

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

换句话说:函数是一个对象,这意味着您可以附加任何您想要的属性。

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.

魂归处 2024-11-23 12:20:39

它表明这些函数具有为其定义/设置的附加属性/方法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文