我只是玩了一下 animate()方法。
.animate( properties [, duration] [, easing] [, complete] )
我知道我不必将所有参数都传递给 javascript 中的函数。但我想知道的是 jquery 是如何计算出 function(){ }
引用回调函数的,它实际上是第 4 个参数,而不是缓动字符串(这是第 3 个:rd)?
$('div').animate({ height: '10px' }, 100, function(){ });
最佳答案
有简单的测试来检查类型:
来自 the source code (动画
调用速度
):
jQuery.speed = function( speed, easing, fn ) {
var opt = speed && typeof speed === "object" ? jQuery.extend( {}, speed ) : {
complete: fn || !fn && easing ||
jQuery.isFunction( speed ) && speed,
duration: speed,
easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
};
...
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},
...
type: function( obj ) {
return obj == null ?
String( obj ) :
class2type[ core_toString.call(obj) ] || "object";
},
...
core_toString = Object.prototype.toString
...
jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
所以基本上它会检查 Object.prototype.toString.call(fn)
是 [object Function]"
。
关于javascript - jQuery 方法,可选参数的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12747152/