JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

/**
 * Инкапсулировал foo
 * protected, потому, что private занят
 */
var ClassA = (function(){

    var protected = {
        foo: Symbol.for("module/ClassA::foo")
    };
    
    function A(i) {
        this[ protected.foo ] += i;
        
        Object.defineProperty(this, 'foo', {
            get: function() { return this[ protected.foo ] },
            set: function(v){},
        });
    }

    A.prototype[ protected.foo ] = "abc";
    
    A.prototype.getResult = function() {
        console.log(this.foo);
    }
    
    return A;
})();

var a = new ClassA("def");
a.getResult();
a.foo = 123;
console.log(a, a.foo);

var b = new ClassA("ghi");
b.getResult();
b.foo = "frfw"; 
console.log(b, b.foo);