JSFiddle - React, Tailwind, and code Playground

by Dan Bonelli

JavaScript

var MyObject = function() {
    var self = this;
    
    var _tripwire = false;
    
    var _value = null;
    
    self.setValue = function(val) {
        if (_tripwire==false)
            _tripwire = true;
        else
            throw "Value is read-only";
            
        _value = val;
    };
    
    self.getValue = function() {
        return val;
    }
};

var o = new MyObject();

o.setValue(1);

try {
    o.setValue(2);
} catch (e) {
    alert("You broke it");
}