JavaScript静态变量

function Student(){
this.name = "khan"; //去掉this和有this的区别
this.info = function(){
alert(
'info()');
}
}

var student = new Student(); //创建Student对象student

document.write(
"对象形式1: "+student.name);

Student.name
= "test"; //声明静态变量

document.write(
"静态访问1:"+Student.name);
var student2 = new Student(); //创建Student对象student2
document.write("对象形式2: "+student2.name);

document.write(
"全局变量:"+name);

1、去掉this后,name为全局变量以对象的形式将不能访问到name的值

2、静态变量不属于类的某个实例对象所独有的属性,为对象的共享

http://www.growup-diary.com/javascript-static-variables.html

posted @ 2011-03-11 14:16  opqrst  阅读(1287)  评论(1编辑  收藏  举报