Created
April 28, 2013 09:07
-
-
Save jinlong/5476364 to your computer and use it in GitHub Desktop.
用 this 定义的方法可以访问局部变量 a,在 prototype 里定义的方法不能访问局部变量 a;
用 this 定义的方法,在 prototype 上定义的方法均可以访问全局变量 b;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
构造器指向了自己,第一次看到,长见识了