1.$.extend({},defaults, options)
The purpose of this is to protect the package default parameters. That is, the parameters in defaults.
The method is to use a new empty object ({}) as the first parameter of $.extend, followed by defaults and the parameter object passed by the user. The advantage of this is that all values are merged into this empty object. On the object, the default value in the plug-in is protected.
$.fn.myPlugin = function(options) { var defaults = { 'color': 'red', 'fontSize': '12px' }; var settings = $.extend({},defaults, options);//将一个空对象做为第一个参数 return this.css({ 'color': settings.color, 'fontSize': settings.fontSize }); }
2. Code obfuscation and compression
The plug-in you download will usually provide a compressed version with the word 'min' in the file name. That is to say, minified, a compressed and condensed version
The compression here does not refer to the functional compression of the code, but by replacing the variable names, method function names, etc. in the code with shorter names, and deleting comments (if any) and deleting code spaces. A condensed version of the whitespace and line breaks. At the same time, since various names in the code have been replaced, others cannot read and distinguish its logic, which also plays a role in confusing the code.
Benefits of compression: 1. Reduce the amount of code, speed up loading, and improve performance
2. Prevent others from stealing the code
The above is the entire content of this article, I hope you all like it.