如果我发布
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/