Order
Most of the most appropriate methods for JavaScript are summarized.
Type
? Original type: We can use the value directly.
Οstring
Οnumber
Οboolean
Οnull
οundefined
Searchindex
var foo = 1,
bar = foo;
bar = 9;
Console.log (foo, bar); => 1, 9
Searchindex
? Compound Type: We access the value indirectly through ' reference '.
Οobject
Οarray
Οfunction
Searchindex
var foo = [1, 2],
bar = foo;
Bar[0] = 9;
Console.log (Foo[0], bar[0]); => 9, 9
Searchindex
Objects
? Creates an object using {}.
Bad
var item = new Object ();
Good
var item = {};
? Do not use reserved words as a keyword.
Searchindex
Bad
var Superman = {
Class: ' Superhero ',
Default: {clark: ' Kent '},
Private:true
};
Good
var Superman = {
Klass: ' Superhero ',
Defaults: {clark: ' Kent '},
Hidden:true
};
Searchindex
Arrays
? Create an array using []
Bad
var items = new Array ();
Good
var items = [];
? If you don't know the length of the array, use Array#push.
Searchindex
var somestack = [];
Bad
Somestack[somestack.length] = ' Abracadabra ';
Good
Somestack.push (' Abracadabra ');
Searchindex
? When you need to copy the array, use Array#slice.
Searchindex
var len = items.length,
Itemscopy = [],
I
Bad
for (i = 0; i < len; i++) {
Itemscopy[i] = items[i];
}
Good
Itemscopy = Items.slice ();
Searchindex
Strings
? For strings, we use single quotes '.
Searchindex
Bad
var name = "Bob Parr";
Good
var name = ' Bob Parr ';
Bad
var fullName = "Bob" + this.lastname;
Good
var fullName = ' Bob ' + this.lastname;
Searchindex
? A string of more than 80 characters, we use the concatenation symbol (\) to let the string display multiple lines.
? Note: Excessive use of characters with concatenated symbols can affect performance.
Searchindex
Bad
var errormessage = ' This 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 = ' This is a super long error '
was thrown because of Batman. \
When you stop to
How Batman had anything to do \
With this, your would get nowhere \
Fast. ';
Good
var errormessage = ' This is a super long error ' +
' was thrown because of Batman. ' +
' When you stop to the ' is about ' +
' How Batman had anything to do ' +
' With this ' and ' would get nowhere ' +
' Fast. ';
Searchindex
? When we are programming, we need to splice a string, we can use Array#join instead of string connection. Especially for IE browsers.
Searchindex
var items,
Messages
length, I;
Messages = [{
State: ' Success ', this article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130709/39005.html
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.