Correct Javascript usage

Source: Internet
Author: User
Tags access properties type casting

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)

// Bad

Var item = new Object ();

// Good

Var item = {};

Do not use reserved keywords as the property name of the object. This will not work in IE8.

// Bad

Var superman = {

Default: {clark: 'kent '},

Private: true

};

// Good

Var superman = {

Ults: {clark: 'kent '},

Hidden: true

};

Array)

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, use the built-in push method of Array to insert data.

Var someStack = [];

// Bad

SomeStack [someStack. length] = 'vein ';

// Good

SomeStack. push ('vein ');


When you want to copy an array, use array. slice

Var len = items. length, // indicates the above content...

ItemCopy = [],

I;

// Bad

For (I = 0; I <len; ++ I ){

ItemCopy [I] = items [I];

}

// Good

ItemCopy = items. slice (); // you should pay attention to it here. 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 ..)

// Bad

Var name = "Bob Parr ";

// Good

Var name = 'Bob parr ';

// Bad

Var fullName = "Bob" + this. lastName;

// Good

Var 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)

// 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 .';


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;

// 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)

// Anonymous function expression ..

Var anonymous = function (){

Return true;

};

// Name function expression.

Var named = function named (){

Return true;

};

// Instantly reference a function

(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 ..

// 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 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 ');


Next, let's proceed ....

Best wishes ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.