浅谈new操作符

来源:互联网 发布:python socket 运用 编辑:程序博客网 时间:2024/05/20 12:49
 var Person = function(name){
  this.name = name;
  this.say = function(){
   return "I am" + this.name;
  }
 }
 var nyf = new Person("nyf");
/* nyf.say();
 以new操作符调用构造函数的时候 发生以下变化
 1.创建一个空对象,并且this变量引用该对象,同时还继承了该函数的原型。
 2. 属性和方法被加入到this引用的对象中
 3.新创建的对象由this所引用,并且最后隐式的返回this
以上情况在new操作符的调用下,后台就相当于
*/
 var Person =function(name){
  //var this = {} 创建this空对象
  this.name = name; //属性加入到this对象中
  this.say = function(){ //方法加入到this对象中
   return "I am" + this.name;
 };
 //return this 隐式的返回this对象
 }
0 0
原创粉丝点击