js 静态私有成员JScript codevar Time {today: ‘2009-3-8′,weather: ‘rain’,show: function(){alert(‘To
js 静态私有成员
- JScript code
var Time = { today: ‘2009-3-8′, weather: ‘rain’, show: function(){ alert(‘Today is ‘ + this.today); } };这种方法只能创建出共有静态成员,不通过new能创建私有静态成员吗?
谢谢
[解决办法]
改成function定义私有变量就好了
- JScript code
function time() { var today = '2009-3-8', weather = 'rain'; this.show = function () { alert('Today is ' + today); }}
[解决办法]
你说的是js中如何使用类?如果是的话你可以参考一下这个帖子:
js中的类
[解决办法]
- HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript">var Time = (function() { var today = '2010-3-8'; var weather = 'rain'; return { show: function() { alert('Today is ' + today); } };})();Time.show();</script></head><body></body></html> 