zoukankan      html  css  js  c++  java
  • js 属性类型之访问器属性

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    	</head>
    	<body>
    		<script type="text/javascript">
    			//2、访问器属性
    			//[[Configurable]],[[Enumerable]],[[Get]],[[Set]]
    			//访问器属性不能直接定义,必须使用Object.definePropertye()来定义
    			var book = {
    				_year: 2004,
    				edition: 1
    			};
    			Object.defineProperty(book , 'year' , {
    				get: function(){
    					return this._year;
    				},
    				set: function(newValue){
    					if(newValue > 2004){
    						this._year = newValue;
    						this.edition += newValue - 2004;
    					}
    				}
    			});
    			book.year = 2008;
    			alert(book.edition);
    			//这是访问器属性常见的方式,即设置一个属性的值导致其他属性发生变化。
    		</script>
    	</body>
    </html>
    

      提取js部分

    //2、访问器属性
    			//[[Configurable]],[[Enumerable]],[[Get]],[[Set]]
    			//访问器属性不能直接定义,必须使用Object.definePropertye()来定义
    			var book = {
    				_year: 2004,
    				edition: 1
    			};
    			Object.defineProperty(book , 'year' , {
    				get: function(){
    					return this._year;
    				},
    				set: function(newValue){
    					if(newValue > 2004){
    						this._year = newValue;
    						this.edition += newValue - 2004;
    					}
    				}
    			});
    			book.year = 2008;
    			alert(book.edition);
    			//这是访问器属性常见的方式,即设置一个属性的值导致其他属性发生变化。
    

      

  • 相关阅读:
    [HDU5184] Brackets
    L2-036 网红点打卡攻略 (25 分)
    L2-017 人以群分 (25 分)
    L2-029 特立独行的幸福 (25 分)
    L2-035 完全二叉树的层序遍历 (25 分)
    L2-031 深入虎穴 (25 分)
    L2-020 功夫传人 (25 分)
    第 50 场双周赛
    L2-027 名人堂与代金券 (25 分)
    L2-024 部落 (25 分)
  • 原文地址:https://www.cnblogs.com/xudy/p/5417366.html
Copyright © 2011-2022 走看看