Javascript jQuery解构|这指的是什么?
这在下面的init函数中指的是什么 当从对象调用方法时,这通常指的是保存该方法的对象 但是在这种情况下,使用new关键字调用该方法 这是否改变了它指向的对象,或者它仍然指向保存方法的对象,在本例中是-Javascript jQuery解构|这指的是什么?,javascript,jquery,Javascript,Jquery,这在下面的init函数中指的是什么 当从对象调用方法时,这通常指的是保存该方法的对象 但是在这种情况下,使用new关键字调用该方法 这是否改变了它指向的对象,或者它仍然指向保存方法的对象,在本例中是-jQuery.fn.init() jQuery解构 jQuery是这样全球化的:(第9422行) jQuery在这里定义:(第42行) jQuery.fn.init在这里定义:(第95行) 当使用new调用函数时,此始终引用新创建的对象实例。在这种情况下,函数恰好是表达式中的对象属性值并不重要。是。
jQuery.fn.init()
jQuery解构
jQuery是这样全球化的:(第9422行)
jQuery在这里定义:(第42行)
jQuery.fn.init在这里定义:(第95行)
当使用
new
调用函数时,此
始终引用新创建的对象实例。在这种情况下,函数恰好是表达式中的对象属性值并不重要。是。函数的此
值始终由调用方式设置。不要这么快提示回复,其他人不一定会等待回复。@hiroprogator不,它不返回函数对象。它只是一个对象。好吧,不管怎样,但是jQuery对象绝对不是一个函数,您可以通过尝试调用一个函数来快速确定它。比如,$('body')()代码>-您将得到一个“不是函数”异常。$()
是一个在内部调用new的函数,然后返回一个函数对象。现在只有我们两个人知道这一点。有关这一点的证明,请参阅我上面的文章。@hiroprotagent:No,$()
不返回函数对象。那样说是完全不准确的。MSDN文档展示了如何创建函数对象,该对象与jQuery返回的内容无关<代码>类型$();//“对象”
函数对象的类型将是“函数”
<代码>类型(函数(x,y){return(x+y);});//“功能”
window.jQuery = window.$ = jQuery;
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
},
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
// Handle $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
}
// Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
// Handle HTML strings
if ( typeof selector === "string" ) {
if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
// Assume that strings that start and end with <> are HTML and skip the regex check
match = [ null, selector, null ];
} else {
match = rquickExpr.exec( selector );
}
// Match html or make sure no context is specified for #id
if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context;
doc = ( context && context.nodeType ? context.ownerDocument || context : document );
// scripts is true for back-compat
selector = jQuery.parseHTML( match[1], doc, true );
if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
this.attr.call( selector, context, true );
}
return jQuery.merge( this, selector );
// HANDLE: $(#id)
} else {
elem = document.getElementById( match[2] );
// Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) {
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) {
return rootjQuery.find( selector );
}
// Otherwise, we inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
}
this.context = document;
this.selector = selector;
return this;
}
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector );
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return this.constructor( context ).find( selector );
}
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
}
return jQuery.makeArray( selector, this );
},
makeArray: function( arr, results ) {
var type,
ret = results || [];
if ( arr != null ) {
// The window, strings (and functions) also have 'length'
// Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
type = jQuery.type( arr );
if ( arr.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( arr ) ) {
core_push.call( ret, arr );
} else {
jQuery.merge( ret, arr );
}
}
return ret;
},