JSFiddle - React, Tailwind, and code Playground

by uedatakuya

JavaScript

function Hoge(init) {
    var value = init;
    var callback = null;
    
    this.get = function() {
        return value;
    };
    
    this.set = function(v) {
        value = v;
        if (typeof callback === "function") {
            callback(value);
        }
    };
    
    this.setCallback = function(c) {
        if (typeof c === "function") {
            callback = c;
        }
    };
}

var hoge1 = new Hoge(100);
// 1
console.log(hoge1.get());

var hoge2 = new Hoge(200);
// 2
console.log(hoge2.get());

hoge2.setCallback(function(v) {
    // 3
    console.log(v);
    // 4
    console.log(hoge2.get())
});
    
hoge2.get = hoge1.get;
//hoge2.set = hoge1.set;
hoge2.set(400);