What does this Code do?
我正在阅读jQuery的"插件/创作",虽然我已经写了一些jQuery-Plugins。 现在我看到jQuery有一种特殊的方法来确定方法并调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | (function( $ ){ var methods = { init : function( options ) { // THIS }, show : function( ) { // IS }, hide : function( ) { // GOOD }, update : function( content ) { // !!! } }; $.fn.tooltip = function( method ) { // Method calling logic if ( methods[method] ) { return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); } else if ( typeof method === 'object' || ! method ) { return methods.init.apply( this, arguments ); } else { $.error( 'Method ' + method + ' does not exist on jQuery.tooltip' ); } }; })( jQuery ); |
我理解最终会发生什么的概念......但究竟是怎么回事? 这部分让我感到困惑:
1 2 3 4 5 6 | // Method calling logic if ( methods[method] ) { return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); } else if ( typeof method === 'object' || ! method ) { return methods.init.apply( this, arguments ); } |
为什么
谢谢!
1 2 3 4 5 6 7 8 | function foo() { var index; for (index = 0; index < arguments.length; ++index) { alert(arguments[index]); } } foo("one","two"); // alerts"one", then alerts"two" |
当我说
1 2 3 4 5 6 7 8 | function foo(namedArg, anotherNamedArg) { alert(namedArg === arguments[0]); // alerts true, of course alert(anotherNamedArg === arguments[1]); // also alerts true namedArg ="foo"; alert(arguments[0]); // alerts"foo" arguments[0] ="bar"; alert(namedArg); // alerts"bar" } |
请注意,将值分配给
1 2 3 | function foo() { alert(foo === arguments.callee); // alerts true } |
但是,出于几个原因,最好避免使用
(有些实现也有
关于
1 | return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); |
正在做的是使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // A function to test with function foo(msg, suffix) { alert(this.prefix +":" + msg + suffix); } // Calling the function without any `this` value will default `this` // to the global object (`window` on web browsers) foo("Hi there","!"); // Probably alerts"undefined: Hi there!" because the // global object probably doesn't have a `prefix` property // An object to use as `this` var obj = { prefix:"Test" }; // Calling `foo` with `this` = `obj`, using `call` which accepts the arguments // to give `foo` as discrete arguments to `call` foo.call(obj,"Hi there","!"); // alerts"Test: Hi there!" // ^----^-----------^---- Three discrete args, the first is for `this`, // the rest are the args to give `foo` // Calling `foo` with `this` = `obj`, using `apply` which accepts the arguments // to give `foo` as an array foo.apply(obj, ["Hi there","!"]); // alerts"Test: Hi there!" // ^---------------^---- Note that these are in an array, `apply` // takes exactly two args (`this` and the // args array to use) |
如果它找不到
例如:
-
.tooltip({ thing: value }) 会调用init({ thing: value }) ,因为这是默认值 -
.tooltip('show', var1, var2) 会调用show(var1, var2)
最终结果是,当你调用
变量
1 2 3 4 5 | function variablesCount() { alert(arguments.length); } variablesCount(var1, var2); // alerts 2 |