Skip to content

Instantly share code, notes, and snippets.

@jinlong
Created April 28, 2013 09:07
Show Gist options
  • Save jinlong/5476364 to your computer and use it in GitHub Desktop.
Save jinlong/5476364 to your computer and use it in GitHub Desktop.
用 this 定义的方法可以访问局部变量 a,在 prototype 里定义的方法不能访问局部变量 a; 用 this 定义的方法,在 prototype 上定义的方法均可以访问全局变量 b;
var ClassA = function(){
var a = 111;
this.b = 222;
this.show = function(){
alert(this.b)
}
};
ClassA.prototype.constructor = ClassA;
ClassA.prototype.showme = function(){
alert(this.b)
}
var aa = new ClassA();
aa.show();
aa.showme();
var ClassA = function(){
var a = 111;
this.b = 222;
this.show = function(){
alert(a)
}
};
ClassA.prototype.constructor = ClassA;
ClassA.prototype.showme = function(){
alert(a)
}
var aa = new ClassA();
aa.show();
aa.showme();
@binnng
Copy link

binnng commented Apr 28, 2013

构造器指向了自己,第一次看到,长见识了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment