Explain this ColorBox code in detail

I am trying to learn the real detailed details of Javascript, so I would appreciate it if anyone could explain this code to me. In ColorBox, the author defines his public method as follows:

publicMethod = $.fn[colorbox] = $[colorbox] = function (options, callback) {
    // do stuff...
};

Then, other public methods are defined, such as:

publicMethod.remove = function () {
    // do more stuff
};

In practice, I know that this function can then be called as $ .colorbox () and $ .colorbox.remove (), but I'm a bit confused by the actual syntax. In particular, what happens when he assigns "$. Fn [colorbox]" and "$ [colorbox]" to publicMethod?

Do you have any comments on this code? Is this a good design? Are there any other patterns you would recommend?

+3
source share
3

JavaScript , . , obj.prop = 1 obj["prop"] = 1 - . , - , . , $["colorbox"] = function() {...} colorbox $ ($ JavaScript), $.colorbox(). , "colorbox", , , , , colorbox "colorbox".

, . remove .

+3
+1

, , . , (colorbox) minifier, .

JavaScript ( , ) .

$.colorbox, $.fn.colorbox publicMethod . publicMethod (.. publicMethod.remove = function() {};), $.fn.colorbox .

$. fn.pluginName is the convention for jQuery plugins, and $ .colorbox is just an alias for that. publicMethod was an abbreviation for the $ .fn.colorbox used in the plugin, which would be abbreviated when the script passed through the minifier.

+1
source

All Articles