javascript - jQuery 构造函数和初始化

标签 javascript jquery firebug

如果我发布

console.dir(jQuery.prototype)

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

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

最佳答案

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

你可以在 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]));
}

输出

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

关于javascript - jQuery 构造函数和初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6447217/

相关文章:

javascript - THREE.js - 像轨迹球控件一样旋转相机

javascript - 阻止 float 标签重置回原始位置

javascript - $(window).height() 值在 Firefox 22.0 中是常量

jquery - 如何自定义 HTML/CSS 工具提示?

html - 如何在 firebug 中遍历 HTML DOM?

javascript - Firebug 不显示我的 javascript?找不到错误

javascript - 如何为 Firebug 创建扩展

javascript - 从 PHP 文件中提取要显示的单选按钮的值

javascript - 循环发送消息

javascript - 如何获取未设置宽度和高度的img元素的宽度和高度?