欢迎来到Jeremy的博客

  1. function CreatPerson(name,age,job){  
  2.         this.name = name;  
  3.         this.age = age;  
  4.         this.job = job;  
  5.         this.sayName = function(){  
  6.             console.log(this.name);  
  7.         }  
  8.     }  
  9.   
  10.     var person1 = new CreatPerson("Nicholas",18,"Software Engineer");  
  11.     var person2 = new CreatPerson("Greg",27,"Doctor");  

1.创建一个新对象

2.将构造函数的作用域赋给新对象(因此this就指向了这个新对象)

3.执行构造函数中的代码(为这个新对象添加属性)

4.返回新对象

posted on 2018-02-26 11:24  jeremy_o  阅读(152)  评论(0编辑  收藏  举报