我一直在使用iPhone6和6S,我发现第一种方法只在6S上有效,而第二种方法在两种情况下都有效。(两者都使用IOS 9.2.1)谁能解释一下它们之间的区别:
$(document).ready(function()
和
$(document).ready(function($)
发布于 2016-02-23 10:39:52
$(document).ready(function() {
console.assert(jQuery == arguments[0] , 'my first param is jquery object')
console.assert(this == document , 'my this is document')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
除了命名第一个参数没有什么不同,试一下:
$(document).ready(function($) {
console.log(arguments)
和
$(document).ready(function() {
console.log(arguments)
})
Javascript没有基于参数的方法重载。
更新:不管您传递的是什么匿名(或命名函数)定义,jquery都会这样调用您的函数:
func.apply(document, jquery)
请参阅来源:https://github.com/jquery/jquery/blob/99e8ff1baa7ae341e94bb89c3e84570c7c3ad9ea/src/callbacks.js#L80
因此,您的第一个参数是将其命名为$
还是不将其命名为jquery对象,并且您的this
将为document
https://stackoverflow.com/questions/35567737
复制相似问题