[b]1、[/b]
[b]2、[/b]
[b]3、[/b]
[b]4、[/b]
[img]http://dl2.iteye.com/upload/attachment/0090/2420/9f4053c6-4a3c-3304-87b8-3d177347ad43.png[/img]
参考:
[url]http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html[/url]
-
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function A(){
var a=1;
}
A.prototype.get=function(){
return a;
}
A.prototype.set=function(value){
a=value; //这一句,a貌似成了全局变量了
}
var a1=new A();
var a2=new A();
a1.set(10);
a1.get()//10
a2.get()//10
console.log(a);//10
</script>
</body>
</html>
[b]2、[/b]
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function A(){
var a=1;//此句无用处(a虽在 function A 内,但非 function A 的一个属性)。
}
A.prototype.get=function(){
return this.a;
}
A.prototype.set=function(value){
this.a=value;
}
var a1=new A();
var a2=new A();
a1.set(10);
a1.get()//10
a2.get()//undefined
console.log(a);//undefined
</script>
</body>
</html>
[b]3、[/b]
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function A(){
this.a = 1;
}
A.prototype.get=function(){
return this.a;
}
A.prototype.set=function(value){
this.a=value;
}
var a1=new A();
var a2=new A();
a1.set(10);
a1.get()//10
a2.get()//1
</script>
</body>
</html>
[b]4、[/b]
[img]http://dl2.iteye.com/upload/attachment/0090/2420/9f4053c6-4a3c-3304-87b8-3d177347ad43.png[/img]
参考:
[url]http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html[/url]
-