Design of libraries and APIs:
- Keep a good habit in the design of parameters: such as order, width,height;top,right,bottom,left;
- Consider undefined as no value and not as a non-specific value;
- In a place where 0, an empty string, and so on are valid parameters, do not pass the Truth Test (| | ) to implement the default value of the parameter;
Use//var x = = = undefined? 0:x;
- Optional object can be used when the multi-parameter object is accepted;
- Extend can be used when working with multiple presets
function extend (target, source) {if (source) {= (var key in source) {var val = source[key];if (typeof val!== ' undefined ') {Target[key] = val;}}} return target;} function Alert (parent, opts) {opts = Extend ({width:320,height:240}, opts); opts = Extend ({x: (PARENT.WIDTH/2)-(Opts.wid th), Y: (PARENT.HEIGHT/2)-(Opts.height), title: ' Alert ', icon: ' Info ', modal:false}, opts); Extend (this, opts);} var alert = new Alert ({width:1200,height:1000},{title: ' child ', modal: ' true '});
- Use a stateless API whenever possible;
JavaScript High-quality code 04