Today, I found a github on how to use JavaScript correctly for our program development. I have a shameless to the original AH. Hang on, let's 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 elevation
Conditional Expressions & Equality//conditional expressions and equations.
Blocks//Block Code
Comments//comment
whitespace//space
Commas//comma
semicolons/semicolon
Type Casting & coercion//class Type conversion
Naming conventions//naming rules
Accessors//Access
Constructors//builder
Events//Time
Modules//model
JQuery//
ECMAScript 5 Compatibility//ECMA 5 compliant
Testing//test
Performance//performance
Resources/resource
in the Wild
T Ranslation
The JavaScript Style Guide Guide
Contributors
License
Types (Type)
Original type: When accessing an original type, the content of the original type is actually accessed directly.
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 of data type, the value of the variable is actually accessed by reference.
Object
Array
function
var foo = [1,2];
bar = foo;
Bar[0] = 9;
Console.log (Foo[0],bar[0]); => 9,9
Object (Objects)
use object literals to create objects (literal)
Bad
var item = new Object ();
Good
var item = {};
Do not use reserved keywords as property names for objects. This cannot work under the IE8.
Bad
var Superman = {
default: {clark: ' Kent '},
private:true
};
Good
var Superman = {
defaults: {clark: ' Kent '},
hidden:true
};
Array (arrays)
also use the literal method to create an array
Bad
var items = new Array ();
Good
var items = [];
If you do not know the length of the array, then use the array's built-in method push to insert the operation
var somestack = [];
Bad
somestack[somestack.length] = ' vein ';
Good
somestack.push (' vein ');
When you want to copy an array, use the Array.slice
var len = items.length,//refers to the above content ...
Itemcopy = [],
i;
Bad for
(i = 0; i < len; ++i) {
itemcopy[i] = items[i];
}
Good
itemcopy = Items.slice ();//Here's the note. I really don't know ...
Strings string
use single quotes to enclose the string ...//Here I did not find a suitable explanation for the performance, I personally like to use it, (wear less than the dress more beautiful point bar.) You know.
Bad
var name = "Bob Parr";
Good
var name = ' Bob Parr ';
Bad
var fullName = "Bob" + this.lastname;
Good
var fullName = ' Bob ' + this.lastname;
Strings that are longer than 80 characters need to be written in multiple lines using string concatenation ... Note that if used excessively, the connection string will affect performance (performance)
Bad
var errormessage = ' Here is a super long error ', ' is ' thrown because of Batman. When you are at stop to be about how Batman had anything to do with this and you are would get nowhere fast. '
Bad
var errormessage = ' It ' a super long error that was thrown because \ of
Batman. When you are at stop to be about how Batman had anything
to do and this is the would get nowhere \
fast.
Good
var errormessage = ' is ' A super long error, is thrown because ' +
' of Batman. When you are at stop to I am about how Batman had anything to do ' +
' and this, your would get nowhere fast. '
If there is a plan to build an array, like the following. It's better to use the array.join effect.
var items,
messages,
length,
i;
Messages = [{
stat: ' Success ', message
: ' This one worked '
},{
stat: ' Success ', message
: ' This one wo Rked '
},{
stat: ' Success ', message
: ' This one worked '
}
];
length = Messages.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> ' + items.join (' </li><li> ') + ' </li></ul> ';
}
function (Functions)
An anonymous function expression ...
var anonymous = function () {return
true;
};
A named function expression.
var named = function named () {return
true;
};
Instant reference function (function
() {
console.log (' Welcome to the Internet. Please follow me. ')
();
Never define a function in a block code (IF,WHILE) that is not a function. Correspondingly, the middle function in the code block assigns the value to the external variable name ...
Bad
if (currentUser) {
function test () {
console.log (' Nope. ');
}
}
Good
var test;
if (currentUser) {
test = function () {
console.log (' Yup ');
};//be careful with the semi-colon.
}
Properties (Attributes)
Use the point syntax to access the property.
var Luke = {
jedi:true,
age:28
};
Bad
var isjedi = luke[' Jedi '];
Good
var isjedi = Luck.jedi;
When you use a variable to access an object property, use [] square brackets to access the
var Luke = {
jedi:true,
age:28
};
function Getprop (prop) {return
luke[prop];
}
var Isjedi = Getprop (' Jedi ');