首页 > 代码库 > 初学JavaScript须知道的七件事

初学JavaScript须知道的七件事

1.1、缩略标记

//定义对象                      //var car = new Object();            //car.colour = ‘red‘;            //car.wheels = 4;            //car.hubcaps = ‘spinning‘;            //car.age = 4;                        //上述代码的简洁写法:            var car = {                colour: red,                wheels: 4,                hubcaps: spinning,                age: 4            }//定义数组            var testArr = new Array(str1, str2, str3, str4);            //上述代码的简洁写法:            var testArr = [str1, str2, str3, str4];   //关联数组            var car = new Array();            car[colour] = red;            car[wheels] = 4;            car[hubcaps] = spinning;            car[age] = 4;     
//三重标记法
      var direction;
      if(x<200){ direction=1;}else{direction=-1}
      //用三重标记法可写为
      var direction=x<200?1:-1;

1.2、JSON数据格式

JSON是Javascript Object Notion的缩写

       //描述乐队        var band = {                "name": "The Red Hot Chili Peppers",                "members": [                    {                        "name": "zhangsan",                        "role": "lead vocals"                    }, {                        "name": "lisi",                        "role": "bass guitar,trumpet,backing vocals"                    }                ],                "year":"2009"            }

  JSON-P 跨域应用:http://www.cnblogs.com/chopper/archive/2012/03/24/2403945.html

初学JavaScript须知道的七件事