您的位置:首頁技術文章
文章詳情頁

javascript - Object.define 可以監聽object變化,那基本類型變量如何監聽

瀏覽:122日期:2023-09-05 13:04:00

問題描述

var obj = {};Object.defineProperty(obj, 'name', { get : function(){ return this._name;}, set: function(val){ this._name = val;}});

那如何監聽 var string='a' 的string變量變化呢?

問題解答

回答1:

Object.defineProperty(window, 'mystring', { get : function(){ console.log('get');return this._name;}, set: function(val){ console.log('set');this._name = val;}});

標簽: JavaScript
国产综合久久一区二区三区