- <SCRIPT LANGUAGE="JavaScript">
- //静态属性:静态属性又称公共属性,它不属于某个类的实例,而是直接属于某个类。
- function user(name)
- {
- this.Name = name ; //实例属性
- }
- user.prototype.age = 0; //静态属性
- user.prototype.birthday = function(){//静态方法
- user.prototype.age++;
- }
- var XiaoWang = new user("小王");//小王
- var XiaoLiu = new user("小刘");//小刘
- alert(XiaoWang.age); // 0 ;
- alert(XiaoLiu.age); // 0 ;
- XiaoWang.birthday(); // 小王过生日了。
- alert(XiaoWang.age); // 1 ;
- alert(XiaoLiu.age); // 1 ;
- /*
- 你会发现两者的 age 属性都是1,也就是说
- user.prototype.age++;改变了会影响到各个实例的相应属性,
- */
- /*
- 如果实例找不到 自己的属性, 会去原型中寻找 属性,
- */
- </SCRIPT>

关于本站 - 免责声明 - 友情链接 - 提点建议 - 联系我们 - 链接纠错 - 广告投放
Copyright © 2008 All rights reserved.