Track whether object changed

前端 未结 5 454
感情败类
感情败类 2021-01-12 23:33
// A simple Javascript Object / String
var object = \"hello\";

// Imagine a button in the DOM, and if it\'s clicked the object value will change
document.getElement         


        
5条回答
  •  梦毁少年i
    2021-01-13 00:31

    Well you can make it an actual oject with get and set methods:

    // A simple Javascript Object 
    var object = new (function(){
        this.text = 'hello';
    
        this.setText = function(msg){
             this.text = msg
             //on change event
        }
    })();
    
    // Imagine a button in the DOM, and if it's clicked the object value will change
    document.getElementById("button").onclick = function(){
       object.setText('New Text');
    }
    

    Here is a demo fiddle: http://jsfiddle.net/maniator/BSYpz/

提交回复
热议问题