站长资源网络编程
js类中的公有变量和私有变量
简介在cnblogs上看了关于js的一些文章 ,做下笔记: 先看代码1: function car(){ var wheel = 3;//私有变量 this.wheel = 4;//公有变量 alert(wheel); alert(this.wheel); } var c
在cnblogs上看了关于js的一些文章 ,做下笔记:
先看代码1:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
alert(wheel);
alert(this.wheel);
}
var car1 = new car();结果是:3 4
代码2:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
}
var car1 = new car();
alert(car1.wheel);结果:4
var wheel = 3是局部变量,this.wheel=4是公有变量,若想访问car中的私有变量,请看代码3:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
this.getPrivateVal = function(){
return wheel;
}
}
var car1 = new car();
alert(car1.getPrivateVal());结果:3
先看代码1:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
alert(wheel);
alert(this.wheel);
}
var car1 = new car();结果是:3 4
代码2:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
}
var car1 = new car();
alert(car1.wheel);结果:4
var wheel = 3是局部变量,this.wheel=4是公有变量,若想访问car中的私有变量,请看代码3:
function car(){
var wheel = 3;//私有变量
this.wheel = 4;//公有变量
this.getPrivateVal = function(){
return wheel;
}
}
var car1 = new car();
alert(car1.getPrivateVal());结果:3
上一篇:js树形控件脚本代码