JS
如何让js实现类似php的 __set __get方法?
1 2 3 4 5 6 | html<input type= "text" id= "ipt1" name= "username" value= "1" _tipsTxt= "提示内容" />
<script type= "text/javascript" >
var username = document.getElementById( 'ipt1' );
alert(username._tipsTxt);
alert(username._maxLen);
</script>
|
登录后复制
登录后复制
属性
回复内容:
JS
如何让js实现类似php的 __set __get方法?
1 2 3 4 5 6 | html<input type= "text" id= "ipt1" name= "username" value= "1" _tipsTxt= "提示内容" />
<script type= "text/javascript" >
var username = document.getElementById( 'ipt1' );
alert(username._tipsTxt);
alert(username._maxLen);
</script>
|
登录后复制
登录后复制
属性
估计你是想要这个:
1 2 | username.__defineGetter__( '_tipsTxt' , function (){ return this.getAttribute( '_tipsTxt' );});
username.__defineSetter__( '_tipsTxt' , function (txt){this.setAttribute( '_tipsTxt' , txt);});
|
登录后复制
JS属性自带set,get功能啊..=_= 请看DEMO:http://jsfiddle.net/7jfmafkd/
具体请查阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty