实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Accessor Properties Example</title>
5
    <script type="text/javascript">
6
7
        var book = {
8
            _year: 2004,
9
            edition: 1
10
        };
11
          
12
        //legacy accessor support
13
        book.__defineGetter__("year", function(){
14
            return this._year;    
15
        });
16
        
17
        book.__defineSetter__("year", function(newValue){
18
            if (newValue > 2004) {
19
                this._year = newValue;
20
                this.edition += newValue - 2004;
21
            }    
22
        });
23
24
        
25
        book.year = 2005;
26
        alert(book.edition);   //2
27
28
    </script>
29
</head>
30
<body>
31
    <p>Note: this example only works in browsers that have legacy getter/setter support (Firefox, Safari 3, Chrome, Opera 9.5).</p>
32
</body>
33
</html>