• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
smileyqp
https://github.com/smileyqp
博客园    首页    新随笔    联系   管理    订阅  订阅

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;
    }
    }
posted @ 2018-09-25 10:23  smileyqp  阅读(109)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3