您的位置:首页技术文章
文章详情页

javascript - Object.define 可以监听object变化,那基本类型变量如何监听

【字号: 日期:2023-09-05 13:04:00浏览:44作者:猪猪

问题描述

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