一佳一

记录像1+1一样简洁的代码

导航

javascript 对象

Posted on 2018-06-20 15:23  一佳一  阅读(166)  评论(0编辑  收藏  举报

定义:

function Person(first, last, age, gender, interests) {
  this.name = {
    first,
    last
  };
  this.age = age;
  this.gender = gender;
  this.interests = interests;
  this.bio = function() {
    alert(this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
  };
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name.first + '.');
  };
};

使用:

var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);
person1.name
person1.greeting()