JS学习笔记——对象

新建对象

  1. 直接创建实例
    1
    2
    3
    4
    5
    6
    7
    //实例化后添加属性
    car=new Object();
    car.type="limousine";
    car.brand="BMW";

    //使用对象literals
    car=[type:"limousine",brand:"BMW"];
  2. 使用构造器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    function Car(type,brand){
    //对象属性
    this.type=type;
    this.brand=brand;

    //对象方法
    function setType(type){
    this.type=type;
    }
    }

    //实例化对象
    var newCar=new Car("PJ","BMW");
  3. for ... in ...可遍历对象中的属性
    1
    2
    3
    4
    var person={fname:"Bill",lname:"Gates",age:56};
    for (x in person){
    txt=txt + person[x];
    }
    该段代码引用自w3school.com.cn

Number对象

  1. 数字均为64位浮点数
  2. 数字0起头表示八进制,数字0x起头表示十六进制