1

jQuery插件中的以下函数创建方法是否正确?

(function($){

 $.fn.sample = function() {
     test();
 }  

function test() {
   alert('Inside jQuery plugin');
} 
}(jQuery));
4

1 回答 1

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));
于 2013-06-26T13:16:35.983 回答