[Label] [JavaScript] seven JavaScript tips

Source: Internet
Author: User

Focus: http://www.javascriptkit.com/

Create an object:

var car = new Object ();

Car.colour = ' red ';

Car.wheels = 4;

Car.hubcaps = ' spinning ';

Car.age = 4;

The same can acchieved with:

var car = {

Colour: ' Red ',

Wheels:4,

Hubcaps: ' Spinning ',

Age:4

}

Note: But what happens when do you use invalidUserInSession ? The main gotcha in this notation is IE. Never ever leave a trailing comma before the closing curly brace or you'll be in trouble.

var moviesthatneedbetterwriters = new Array (

' Transformers ', ' Transformers2 ', ' Avatar ', ' Indiana Jones 4 '

);

var moviesthatneedbetterwriters = [

' Transformers ', ' Transformers2 ', ' Avatar ', ' Indiana Jones 4 '

];

Sort the data in an array, using the sort () directly, refer to the article: http://www.javascriptkit.com/javatutors/arraysort.shtml

Notice that the can not use sort () on a number array because it sorts lexically (lexical aspect).

var numbers = [23, 342, 87, 145];

Numbers.sort (function (A, b) {return a B;}); Ascending, ascending

Numbers.sort (function (A, b) {return b-a;}); /descending, Descending

Math.max (123, 3, 2, 433, 4);//math.max () return the largest number form a list of parameters

Because This tests for numbers and returns the largest one, you can use it to test for browser support of Cetain Properti Es.

var scrolltop = Math.max (

Doc.documentElment.scrollTop,

Doc.body.scrollTop

);

[Label] [JavaScript] seven JavaScript tips

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.