JavaScript创建对象的几种方式

  1. 使用new关键字创建;
    person=new Object();
    person.firstname="Bill";
    person.lastname="Gates";
    person.age=56;
    person.eyecolor="blue"; 
  2. 使用替代语法(使用对象 literals);
    person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
     
  3. 使用函数来构造对象;
    function person(firstname,lastname,age,eyecolor)
    {
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
    }
    
    var myFather=new person("Bill","Gates",56,"blue");
    var myMother=new person("Steve","Jobs",48,"green");
     

你可能感兴趣的:(JavaScript)