Today I found a question on github about how to correctly use javascript for our program development. I shamelessly came up with an original... It's a scam. Let's share it with you.
A mostly reasonable approach to Javascript.
Types // Type
Objects //Objects
Arrays //Arrays
Strings //Strings
Functions //Functions
Properties //Properties
Variables //Variables
Hoisting //Variable improvement
Conditional Expressions & Equality // Conditional Expressions and Equalities.
Blocks //Block Code
Comments //Comments
Whitespace //Space
Commas //Commas
Semicolons //Semicolons
Type Casting & Coercion //Type Conversion
Naming Conventions //Naming Rules
Accessors //Access
Constructors //Constructors
Events //Time
Modules //Models
jQuery //
ECMAScript 5 Compatibility //ECMA 5 Compatibility
Testing //Testing
Performance //Performance
Resources //Resources
In the Wild
Translation
The JavaScript Style Guide Guide
Contributors
License
Types (type)
Primitive type: When accessing a primitive type, it actually directly accesses the contents of the primitive type.
string
number
boolean
null
undefined
var foo = 1,
bar = foo;
bar = 9;
console.log(foo,bar); //=> 1,9
Complex type: When you access a complex type data type, you actually access the value of the variable through a reference.
object
array
function
var foo = [1,2];bar = foo;bar[0] = 9;console.log(foo[0],bar[0]); // => 9,9
object(object)
Use object literals to create objects (literal)
//badvar item = new Object();//goodvar item = {};Do not use reserved keywords as the object's property name. This will not work under IE8.
//badvar superman = {default: {clark: 'kent'},private: true};//goodvar superman = {defaults: {clark: 'kent'},hidden: true};array(array)
Also use the literal method to create an array
//badvar items = new Array();//goodvar items = [];
If you don't know the length of the array, then use Array's built-in method push for insertion operation
var someStack = [];//badsomeStack[someStack.length] = 'vein';//goodsomeStack.push('vein');When you want to copy an array, use array.slice
var len = items.length, //refers to the above content...itemCopy = [],i;//badfor(i = 0; i < len ; ++i){itemCopy[i] = items[i];}//gooditemCopy = items.slice(); //I need to pay attention here. I really don't know this...Strings string
Use single quotes to enclose strings...//I haven't found a suitable explanation for performance here, I personally like to use it like this, (It's better to wear less than wear...you know...)
//badvar name = "Bob Parr";//goodvar name = 'Bob Parr';//badvar fullName = "Bob " + this.lastName;//goodvar fullName = 'Bob ' + this.lastName;
When strings are longer than 80 characters, you need to use string concatenation to write on multiple lines. Note that if overuse, concatenation of strings will affect performance.
// badvar errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';// badvar errorMessage = 'This is a super long error that was thrown because /of Batman. When you stop to think about how Batman had anything to do /with this, you would get nowhere /fast.';// goodvar errorMessage = 'This is a super long error that was thrown because ' +'of Batman. When you stop to think about how Batman had anything to do ' +'with this, you would get nowhere fast.';
If you create an array in a planned way, like the following. Using Array.join will work better..
var items,messages,length,i;messages = [{stat: 'success',message: 'This one worked'},{stat: 'success',message: 'This one worked'},{stat: 'success',message: 'This one worked'}];length = messages.length;//badfunction inbox(messages){items = '<ul>';for (i = 0; i < length; i++) {items += '<li>' + messages[i].message + '</li>';}return items + '</ul>';}//goodfunction inbox(messages){items = [];for( i = 0; i < length ; i++){items[i] = messages[i].message;}return '<ul><li>' + items.join('</li><li>') + '</li></ul>';}Functions
//Anonymous function expression..var anonymous = function(){return true;};//Name function expression.var named = function named(){return true;};//Instant reference function(function(){console.log('Welcome to the Internet. Please follow me.');})();Never define functions in non-function block code (if, while). Correspondingly, the function is assigned to the external variable name in the middle of the code block.
//badif(currentUser){function test(){console.log('Nope.');}}//goodvar test;if(currentUser){test = function(){console.log('Yup'); }; //be careful with the semi-colon.}Properties (Properties)
Use dot syntax to access properties.
var luke = {jedi: true,age: 28};//badvar isJedi = luke['jedi'];//goodvar isJedi = lucky.jedi;When accessing object properties using variables, use [] square brackets to access
var luke = {jedi: true,age: 28};function getProp(prop) {return luke[prop];}var isJedi = getProp('jedi');