基本封装方法

请看下面的例子:

var Person = function(name,age){
  this.name = name;
  this.age = age || "未填写";
  this.hobbys = [];}Person.prototype = {
  sayName:function(){
    console.log(this.name);
  },
  sayAge:function(){
    console.log(this.age);
  },
  addHobby:function(hobbys){
    this.hobbys = this.hobbys.concat(hobbys);
  }}var person1 = new Person("Jane","20");var person2 = new Person("TabWeng","21");person1.addHobby(['sing','drawing']);person2.addHobby(['football','study','running']);person1.sayName();console.log(person1.hobbys.toString());person2.sayName();console.log(person2.hobbys.toString());

运行结果:

        		

网友评论