美文网首页
JavaScript 基础之JavaScript对象(四)

JavaScript 基础之JavaScript对象(四)

作者: 桜花約束 | 来源:发表于2018-09-25 10:21 被阅读0次

    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;
      }
      }

    相关文章

      网友评论

          本文标题:JavaScript 基础之JavaScript对象(四)

          本文链接:https://www.haomeiwen.com/subject/oipkoftx.html