首页 > 代码库 > [label][JavaScript]七个JavaScript技巧

[label][JavaScript]七个JavaScript技巧

重点: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 be acchieved with:

var car = {

  colour : ‘red‘,

  wheels: 4,

  hubcaps:‘spinning‘ ,

  age: 4

}

注意:But what happens when 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(),参考文章:http://www.javascriptkit.com/javatutors/arraysort.shtml

Notice that you can not use sort() on a number array because it sorts lexically(词汇方面).

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

numbers.sort(function(a , b){ return a - b;}); //ascending,升序

numbers.sort(function(a , b){return b - a;});//descending,降序

 

Math.max(12 , 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 properties.

var scrollTop = Math.max(

  doc.documentElment.scrollTop,

  doc.body.scrollTop

);

[label][JavaScript]七个JavaScript技巧