Most of the most reasonable methods for Javascript are summarized.
Type
• Original Type: values can be used directly.
Optional string
Serial number
Optional boolean
Limit null
ο undefined
Var foo = 1,
Bar = foo;
Bar = 9;
Console. log (foo, bar); // => 1, 9 • composite type: We use 'quot' to indirectly access the value.
Jsonobject
Arrays array
Function
Var foo = [1, 2],
Bar = foo;
Bar [0] = 9;
Console. log (foo [0], bar [0]); // => 9, 9
Objects
• Use {} to create an object.
// Bad
Var item = new Object ();
// Good
Var item = {};
• Do not use reserved words as keywords.
// Bad
Var superman = {
Class: 'superhero ',
Default: {clark: 'kent '},
Private: true
};
// Good
Var superman = {
Klass: 'superhero ',
Ults: {clark: 'kent '},
Hidden: true
};
Arrays
• Use [] to create an array
// Bad
Var items = new Array ();
// Good
Var items = []; • if you do not know the Array length, use Array # push.
Var someStack = [];
// Bad
SomeStack [someStack. length] = 'abracadaba ';
// Good
SomeStack. push ('abracadaba ');
• When You Need To copy an Array, use Array # slice.
Var len = items. length,
ItemsCopy = [],
I;
// Bad
For (I = 0; I <len; I ++ ){
ItemsCopy [I] = items [I];
}
// Good
ItemsCopy = items. slice ();
Strings
• For strings, we use single quotation marks ''.
// Bad
Var name = "Bob Parr ";
// Good
Var name = 'Bob parr ';
// Bad
Var fullName = "Bob" + this. lastName;
// Good
Var fullName = 'bob' + this. lastName; • strings with more than 80 characters. We use the series symbol (\) to display the strings in multiple lines.
• Note: excessive use of characters with series characters may 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 wowould 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 wocould 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 wocould get nowhere' +
'Fast .';
• When programming, We need to splice a string. We can use Array # join to replace string join. Especially for IE browsers.
Var items,
Messages,
Length, I;
Messages = [{
State: 'success ',
Message: 'This one worked .'
},{
State: 'success ',
Message: 'This one worked as well .'
},{
State: 'error ',
Message: 'This one did not work .'
}];
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> ';
}
Functions
• Function expressions
// Anonymous function expression
Var anonymous = function (){
Return true;
};
// Named function expression
Var named = function named (){
Return true;
};
// Immediately-invoked function expression (IIFE)
(Function (){
Console. log ('Welcome to the Internet. Please follow me .');
}) (); • Never declare a function in a non-function block (if, while. We can convert a function declaration into a function expression.
// Bad
If (currentUser ){
Function test (){
Console. log ('Nope .');
}
}
// Good
If (currentUser ){
Var test = function test (){
Console. log ('Yup .');
};
}
• Do not name a parameter arguments. The arguments parameter is a special variable in the function scope. If you name the parameter arguments, this parameter will overwrite its original special variables.
// Bad
Function nope (name, options, arguments ){
//... Stuff...
}
// Good
Function yup (name, options, args ){
//... Stuff...
}