实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Multiple Properties Example</title>
5
    <script type="text/javascript">
6
7
        var book = {};
8
        
9
        Object.defineProperties(book, {
10
            _year: {
11
                value: 2004
12
            },
13
            
14
            edition: {
15
                value: 1
16
            },
17
            
18
            year: {            
19
                get: function(){
20
                    return this._year;
21
                },
22
                
23
                set: function(newValue){
24
                    if (newValue > 2004) {
25
                        this._year = newValue;
26
                        this.edition += newValue - 2004;
27
                    }                  
28
                }            
29
            }        
30
        });
31
           
32
        book.year = 2005;
33
        alert(book.edition);   //2
34
35
    </script>
36
</head>
37
<body>
38
    <p>Note: this example only works in browsers that have implemented the ECMAScript 5 <code>Object.defineProperty()</code> method (IE9 and Firefox 4).</p>
39
</body>
40
</html>