筆記-記一次簡單的Object.defineProperty的使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<input type="text" id="a"/>
		<span id="b"></span>
	</body>
	<script>
		var obj = {};
		Object.defineProperty(obj,'hello',{
			set:function(newVal){
				console.log(11)
				document.getElementById("a").value = newVal;
				document.getElementById('b').innerHTML = newVal;
			}
		})
		
		document.addEventListener('keyup',function(e){
			obj.hello = e.target.value;
		})
	</script>
</html>
複製代碼
相關文章
相關標籤/搜索