How to use javascript correctly to develop our program

  • 2020-03-30 03:26:57
  • OfStack

Today on github we found an article on how to use javascript correctly for our application development. Pit dad. Bring to share with everybody.
A mostly reasonable approach to Javascript.
Types / / type
Objects / / object
The Arrays / / array
Strings // Strings
Functions provides / / function
The Properties / / Properties
The Variables / / Variables
Hoisting / / variable to ascend
Conditional Expressions & Equality // Conditional Expressions and equations.
Blocks // block code
Comments / / comment
Whitespace / / space
Commas / / comma
Semicolons / / a semicolon
Type Casting & Coercion / / Type conversion
Naming Conventions // Naming rules
Accessors, / / access
Constructors // Constructors
The Events / / time
Modules / / model
JQuery / /
ECMAScript 5 Compatibility //ECMA 5 Compatibility
Testing / / test
The Performance / / Performance
Resources / / Resources
In the Wild
Translation
The JavaScript Style Guide Guide
Contributors
License

Types (Types)
Primitive type: when accessing a primitive type, you actually access the contents of that primitive type directly.
The string
number
Boolean
null
undefined
Var foo = 1,
The bar = foo;
The bar = 9;
The console. The log (foo, bar); / / = > 1, 9

Complex types: when you access a complex type data type, you are accessing the value of the variable by reference.
The object
array
The function


var foo = [1,2];
bar = foo;
bar[0] = 9;
console.log(foo[0],bar[0]); // => 9,9

The object (objects)
Using object literals to create objects


//bad
var item = new Object();
//good
var item = {};

Do not use reserved keywords as property names for objects. This does 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, to insert it


var someStack = [];
//bad
someStack[someStack.length] = 'vein';
//good
someStack.push('vein');

Use array.slice when you want to copy an array


var len = items.length, //That's what it says...
itemCopy = [],
i;
//bad
for(i = 0; i < len ; ++i){
itemCopy[i] = items[i];
}
//good
itemCopy = items.slice(); // Here's the thing to watch out for . I didn't know that ...

Strings Strings
Use single quotes to surround strings... // I can't find a proper performance explanation here, and I like to use it myself. 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 longer than 80 characters need to be written on multiple lines using string concatenation.. Note that the connection string can affect performance if overused


// 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 are creating an Array by design, 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;
//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 (Functions provides)


//Anonymous function expression..
var anonymous = function(){
return true;
};
//Name the function expression.
var named = function named(){
return true;
};
//Immediate reference function
(function(){
console.log('Welcome to the Internet. Please follow me.');
})();

Never define a function in a non-functional block code (if,while).


//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
Use the dot syntax to access properties.


var luke = {
jedi: true,
age: 28
};
//bad
var isJedi = luke['jedi'];
//good
var isJedi = luck.jedi;

When using a variable to access an object property, use [] square brackets to access it


var luke = {
jedi: true,
age: 28
};
function getProp(prop) {
return luke[prop];
}
var isJedi = getProp('jedi');

Related articles: