JavaScript 基础之JavaScript对象(四)

JavaScript 对象

  • 创建了对象的一个新实例,并向其添加了四个属性:
    person=new Object();
    person.firstname=“John”;
    person.lastname=“Doe”;
    person.age=50;
    person.eyecolor=“blue”;
  • 替代语法(使用对象 literals):
    person={firstname:“John”,lastname:“Doe”,age:50,eyecolor:“blue”};
  • 使用函数来构造对象:
    function person(firstname,lastname,age,eyecolor)
    {
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
    }
  • 对象构造创建新的对象实例:
    var myFather=new person(“John”,“Doe”,50,“blue”);
    var myMother=new person(“Sally”,“Rally”,48,“green”);
    function person(firstname,lastname,age,eyecolor)
    {
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
    this.changeName=changeName;
    function changeName(name)
    {
    this.lastname=name;
    }
    }
原文地址:https://www.cnblogs.com/smileyqp/p/12675402.html