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>

属性

javascriptprototype
阅读 3.9k
2 个回答

估计你是想要这个:

username.__defineGetter__('_tipsTxt', function(){return this.getAttribute('_tipsTxt');});
username.__defineSetter__('_tipsTxt', function(txt){this.setAttribute('_tipsTxt', txt);});
推荐问题