我创建了一个插件,但想确定我对“本地”函数的作用。
这是我所做的示意图:
(function($) {
var methods = {
init : function( options ) {
// CODE ...
// Call of a local function
_test( this );
// CODE .....
},
destroy : function( ) {
// CODE .....
_test( this );
// CODE .....
}
};
function _test( container ) {
// My code : example :
$(container).append("<div id='myplugin'></div>");
}
$.fn.myplugin = 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 );
}
else {
$.error( 'Method ' + method + ' does not exist on jQuery.myplugin' );
}
};
})(jQuery);
如您所见,我没有直接在方法函数中插入代码,而是在其他_函数中。_functions 是否可以被视为插件的本地或私有功能?我没有成功在插件之外调用它们,所以对我来说它们似乎可以被视为私有函数......
我是否总是将我的代码直接放在方法对象的函数中?如何声明将在多个方法中使用的函数?
那么命名空间呢?真不明白。
谢谢 !