我对 Object.defineProperty()的理解;

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<button id="btn">111</button>
	<script>
		var ob = {};
		var count=12;
		Object.defineProperty(ob,"name",{
			get:function(){
				console.log("这个人来获取值了!!");
				return count;
			},
			set:function(va){
				console.log("这个人来设置值了!!");
				count = va+1;
			}
		});

		//ob.name = "11111";
		console.log(ob.name);
		ob.name = "1111111111";
		console.log(ob.name);

	</script>
</body>
</html>

发表评论

邮箱地址不会被公开。