Proceed to the first two articles in sequence. This article serves as the final article. Blocks • has {} code, which can be processed by line breaks.
// badif (test) return false;// goodif (test) return false;// goodif (test) { return false;}// badfunction() { return false; }// goodfunction() { return false;}
Comments • Use/**... */for multi-line comments /**...*/. Contains the description, parameter type, and return value.
// bad// make() returns a new element// based on the passed in tag name//// @param <String> tag// @return <Element> elementfunction make(tag) { // ...stuff... return element;}// good/** * make() returns a new element * based on the passed in tag name * * @param <String> tag * @return <Element> element */function make(tag) { // ...stuff... return element;}
• Use // For Single Row comments //. A single line comment is placed on a new line separately. Place a blank line before the comment.
// badvar active = true; // is current tab// good// is current tabvar active = true;// badfunction getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type;}// goodfunction getType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type'; return type;}
• Add fixme or todo before commenting on some issues, which will help developers quickly understand the code intent.
• Use // fixme: annotate the problem
function Calculator() { // FIXME: shouldn't use a global here total = 0; return this;}
• Use // todo: comment out the solution to the problem
function Calculator() { // TODO: total should be configurable by an options param this.total = 0; return this;}
Type Casting & coercion • Execute forced type conversion before declaration. • String
// => this.reviewScore = 9;// badvar totalScore = this.reviewScore + '';// goodvar totalScore = '' + this.reviewScore;// badvar totalScore = '' + this.reviewScore + ' total score';// goodvar totalScore = this.reviewScore + ' total score';
• For numeric conversion, use parseint with the base number of type conversion.
• If parseint becomes your bottleneck and is in performance, you need to use the "shift" operation. Then, write down a comment to explain why you did this.
VaR inputvalue = '4'; // badvar val = new number (inputvalue); // badvar val = + inputvalue; // badvar val = inputvalue> 0; // badvar val = parseint (inputvalue); // goodvar val = Number (inputvalue); // goodvar val = parseint (inputvalue, 10 ); // good/*** parseint slows down my code. * To increase the speed, use the displacement operation to forcibly convert a string to a number. */Var val = inputvalue> 0;
• Boolean
var age = 0;// badvar hasAge = new Boolean(age);// goodvar hasAge = Boolean(age);// goodvar hasAge = !!age;
Constructors • extends objects using methods instead of using a new object.
function Jedi() { console.log('new jedi');}// badJedi.prototype = { fight: function fight() { console.log('fighting'); }, block: function block() { console.log('blocking'); }};// goodJedi.prototype.fight = function fight() { console.log('fighting');};Jedi.prototype.block = function block() { console.log('blocking');};
• Let the object method return this, which facilitates the Chain Lock operation of the method.
// badJedi.prototype.jump = function() { this.jumping = true; return true;};Jedi.prototype.setHeight = function(height) { this.height = height;};var luke = new Jedi();luke.jump(); // => trueluke.setHeight(20) // => undefined// goodJedi.prototype.jump = function() { this.jumping = true; return this;};Jedi.prototype.setHeight = function(height) { this.height = height; return this;};var luke = new Jedi();luke.jump() .setHeight(20);
• We can customize a tostring () method. -- Make sure it runs normally without any other impact.
function Jedi(options) { options || (options = {}); this.name = options.name || 'no name';}Jedi.prototype.getName = function getName() { return this.name;};Jedi.prototype.toString = function toString() { return 'Jedi - ' + this.getName();};
The conclusion is finally completed, hoping to help everyone. Recommendation