Access private members of jQuery plugin

后端 未结 3 842
南方客
南方客 2020-12-19 08:19

jQuery plugins use a pattern like this to hide private functions of a plugin:

(function ($) {
    var a_private_function = function (opts) {
        opts.onS         


        
3条回答
  •  太阳男子
    2020-12-19 09:07

    What you have there is a classical example of a closured function.

    a_private_function is a function which is only visible within the scope from the "outer" anonymous function. Because of closure, the anonymous function assigned to name_of_plugin has access to the outer context and therefore a_private_function.

    This is a good thing since you can protect and hide some of functions and variables.

    Short story, there is absolutly zero chance to access a closured variable from the outside.

提交回复
热议问题