jQuery插件中的以下函数创建方法是否正确?
(function($){
$.fn.sample = function() {
test();
}
function test() {
alert('Inside jQuery plugin');
}
}(jQuery));
答案 0 :(得分:1)
是的,它是“正确的”,因为它是有效的,test
只能在包装函数中访问:
// Immediately-invoked function expression, creates a new execution context
(function($){
// Expose a property on jQuery.prototype, available to all jQuery instances
$.fn.sample = function() {
test();
}
// "Private" function, only accessible inside the enclosing context
function test() {
alert('Inside jQuery plugin');
}
}(jQuery));