我快速搜索了一下 Firebug DOM 选项卡着色的含义,我看到了下面的解释 from here
Red colored bold text points "constructor function"
Green colored bold text points "user function".
这两种函数有什么区别?
最佳答案
jQuery
函数是构造函数($
只是对jQuery
的引用)。当你看到它的定义时,你可以看到:
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( …
您可以在定义构造函数时重现此行为(粗体红色)并向其原型(prototype)对象添加一些内容,例如
var testFunc = function () {
/* nothing so far */
};
testFunc.prototype.baz = function () {
/* nothing, this gets boring */
};
甚至只是一个随机数
testFunc.prototype.baz = 4;
请注意,这不符合 constructor function 的实际定义在 JavaScript 中。如果您在 Firebug 中测试链接的源代码,car
将显示为绿色,而不是红色。此外,参见 The Benefits of JavaScript Prototype .
关于javascript - Firebug 构造函数与用户函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972243/