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 equations.
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;
(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; (foo[0],bar[0]); // => 9,9
object(object)
Use object literal to create an object (literal)
//bad var item = new Object(); //good var item = {};
Do not use reserved keywords as the object's property name. This will not work under IE8.
//bad var superman = { default: {clark: 'kent'}, private: true }; //good var superman = { defaults: {clark: 'kent'}, hidden: true };
array(array)
Also use the literal method to create an array
//bad var items = new Array(); //good var items = [];
If you don't know the length of the array, use Array's built-in method push for insertion operation
var someStack = []; //bad someStack[] = 'vein'; //good ('vein');
When you want to copy an array, use
var len = , //Refers to the above content...itemCopy = [], i; //bad for(i = 0; i < len ; ++i){ itemCopy[i] = items[i]; } //good itemCopy = (); //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...)
//bad var name = "Bob Parr"; //good var name = 'Bob Parr'; //bad var fullName = "Bob " + ; //good var fullName = 'Bob ' + ;
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.
// bad var 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.'; // bad var 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.'; // good var 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 have a plan, create an array, like the following. The effect will be better if you use it...
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 = ; //bad function inbox(messages){ items = '<ul>'; for (i = 0; i < length; i++) { items += '<li>' + messages[i].message + '</li>'; } return items + '</ul>'; } //good function inbox(messages){ items = []; for( i = 0; i < length ; i++){ items[i] = messages[i].message; } return '<ul><li>' + ('</li><li>') + '</li></ul>'; }
Functions
//Anonymous function expression..var anonymous = function(){ return true; }; // Named function expressions.var named = function named(){ return true; }; //Instant reference function(function(){ ('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.
//bad if(currentUser){ function test(){ ('Nope.'); } } //good var test; if(currentUser){ test = function(){ ('Yup'); }; //be careful with the semi-colon. }
Properties (Properties)
Use dot syntax to access properties.
var luke = { jedi: true, age: 28 }; //bad var isJedi = luke['jedi']; //good var isJedi = ;
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');