Today, I found an original article on how to use javascript to develop our programs on github. I am ashamed to come and share it with you.
A mostly reasonable approach to Javascript.
Types // type
Objects // object
Arrays // Array
Strings // string
Functions // Function
Properties // Properties
Variables // variable
Hoisting // variable increase
Conditional Expressions & Equality ity // Conditional Expressions and equations.
Blocks // Block Code
Comments // comment
Whitespace // Space
Commas // comma
Semicolons // semicolon
Type Casting & Coercion // Type conversion
Naming Conventions // Naming rules
Accessors // access
Constructors // Constructor
Events // time
Modules // Model
JQuery //
ECMAScript 5 Compatibility // ECMA 5 Compatibility
Testing // Test
Performance // Performance
Resources // Resource
In the Wild
Translation
The JavaScript Style Guide
Contributors
License
Types (type)
Original Type: when accessing an original type, you can directly access the original type of content.
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 data type, you actually access the value of this variable through reference.
Object
Array
Function
var foo = [1,2];bar = foo;bar[0] = 9;console.log(foo[0],bar[0]); // => 9,9
Object)
Use object literal to create object (literal)
//badvar item = new Object();//goodvar item = {};
Do not use reserved keywords as the property name of the object. This will not work in IE8.
//badvar superman = {default: {clark: 'kent'},private: true};//goodvar superman = {defaults: {clark: 'kent'},hidden: true};
Array)
Use the literal method to create an array.
//badvar items = new Array();//goodvar items = [];
If you do not know the length of the Array, use the built-in push method of Array to insert data.
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 content above... itemCopy = [], I; // badfor (I = 0; I <len; ++ I) {itemCopy [I] = items [I];} // gooditemCopy = items. slice (); // pay attention to this. I really don't know this...
Strings string
Use single quotes to enclose strings... // I have not found a proper explanation about performance here. I personally like this. (wearing less clothes is better than wearing more clothes .. you know ..)
//badvar name = "Bob Parr";//goodvar name = 'Bob Parr';//badvar fullName = "Bob " + this.lastName;//goodvar fullName = 'Bob ' + this.lastName;
When the string is longer than 80 characters, you need to use the string connection to write in multiple lines .. note that if you use it too much, the connection string will affect the performance (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 an Array is created in a planned manner, it will be better to use Array. join as follows ..
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 the function expression. var named = function named () {return true ;}; // instantly reference a function () {console. log ('Welcome to the Internet. please follow me. ');})();
Never define a function in the 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)
Use the dot syntax to access properties.
var luke = {jedi: true,age: 28};//badvar isJedi = luke['jedi'];//goodvar isJedi = luck.jedi;
When using variables to access object properties, use [] square brackets to access
var luke = {jedi: true,age: 28};function getProp(prop) {return luke[prop];}var isJedi = getProp('jedi');