jQueryプラグインのパターンに従って、 apply()を使用してスコープを定義し、これに適用methods.myfunc
した場合、関数、たとえば関数のアリティをどのように見つけることができますか?this
arguments
(function($, window, document ){
"use strict";
//...
methods = {
myfunc: function(){
// myfunc.length? tried, didnt work
// arguments.length? tried, didnt work
// methods.myfunc.length? tried, didnt work
// arguments.callee tried, doesnt work in strict mode
}
//...
}
$.MyPluginThing = function( method ){
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, window );
}else{
$.error( "Method " + method + " does not exist on jQuery.MyPluginThing" );
}
}...
これは、関数スコープに関する私の無知の一部を明らかにするかもしれませんが、私はここでかなり困惑しており、これを十分に説明する例を見つけていません。
この質問に対する私のインスピレーションの一部は、NodeJS / ExpressJSから来ています。ここでは、いくつかの関数に対して可変数の引数があります。たとえば、3つの引数が渡された場合、エラーオブジェクトがあると想定されますが、2つを渡すことも簡単にでき、問題はありません。
更新:コードの関数をinitからmyfuncに変更しました