javascript 如何实现类似 PHP 的 __set __get
JS
如何让JS实现类似PHP的 __set __get方法?
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); //这些类似 _maxLen 是不确定的 </script>
属性
javascript
prototype
无意识电波恋
11 years, 2 months ago
Answers
JS属性自带
set
,
get
功能啊..=_= 请看DEMO:
http://jsfiddle.net/7jfmafkd/
具体请查阅:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
天欲雪TYX
answered 11 years, 2 months ago