日期:2014-05-16  浏览次数:20446 次

怎么才能定义类似“Number.MAX_VALUE”的成员常量?
这个值是不能更改的:
Number.MAX_VALUE = 7;
document.write( Number.MAX_VALUE );
document.write( "<br />" );

function test(){
  this.yyy = 3;
}
test.xxx = 4
document.write( test.xxx );
test.xxx = 7;
document.write( test.xxx );
用户没这个权利么?

------解决方案--------------------

text={get XXX(){return 3}}
text.XXX="abc";
alert(text.XXX);

------解决方案--------------------

var o={};
//IE8以上和其它主流浏览器都支持
Object.defineProperty(o, "firstName", {
//可以在set、get中操作。
set: function (x) {
//x是要赋的值,例如o.firstName='sier',这里x就是'sier'

this.firstname = '2';//传进来的属性是驼峰式,而这里全部小写
},
get: function () {
return this.firstname;//可以返回任何值
}
//其它属性设置

});

alert(o.firstName);//第一次未赋值,undefined
o.firstName='sier';//赋值未成功,因为set函数中将2赋给firstName
alert(o.firstName);//弹出firstName,也可以弹出其它常量。取决于get的return语句

------解决方案--------------------
引用:

text={get XXX(){return 3}}
text.XXX="abc";
alert(text.XXX);

不兼容;
吓到了,以为js实现读写逻辑了呢,原来只是浏览器自己搞的