Home  >  Q&A  >  body text

javascript - Object.define can monitor object changes, how to monitor basic type variables

var obj = {};

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

How to monitor the string variable changes of var string="a"?

仅有的幸福仅有的幸福2713 days ago602

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-06-05 11:14:55

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

    reply
    0
  • Cancelreply