码迷,mamicode.com
首页 > 编程语言 > 详细

[label][JavaScript]七个JavaScript技巧

时间:2014-09-03 19:36:37      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:des   http   os   io   使用   java   ar   for   数据   

重点: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技巧

标签:des   http   os   io   使用   java   ar   for   数据   

原文地址:http://www.cnblogs.com/shuman/p/3954303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!