__defineGetter__()和__defineSetter__()。这两个方法最初是由Firefox 引入的,后来Safari 3、Chrome 1 和Opera 9.5 也给出了相同的实现

2017-06-21 10:36:00
admin
原创
538
<!DOCTYPE html>
<html>
<head>
    <title>Accessor Properties Example</title>
    <script type="text/javascript">

        var book = {
            _year: 2004,
            edition: 1
        };
          
        //legacy accessor support
        book.__defineGetter__("year", function(){
            return this._year;    
        });
        
        book.__defineSetter__("year", function(newValue){
            if (newValue > 2004) {
                this._year = newValue;
                this.edition += newValue - 2004;
            }    
        });

        
        book.year = 2005;
        alert(book.edition);   //2

    </script>
</head>
<body>
    <p>Note: this example only works in browsers that have legacy getter/setter support (Firefox, Safari 3, Chrome, Opera 9.5).</p>
</body>
</html>
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络