js类中的公有变量和私有变量

时间:2008-07-24    点击:155   
在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
js查找父节点的简单方法
一个小型js框架myJSFrame附API使用帮助
JavaScript 给汉字排序实例代码
教你如何解密js/vbs/vbscript加密的编码异处理小结
js树形控件脚本代码
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved