User Tools

Site Tools


Pseudo private attributes in JS

Very pseudo. =)

oop.js
#!/usr/bin/env node
 
'use strict';
 
class A {
    constructor(name) {
        this._name = name;
    }
 
    name(n) {
        if (n) { this._name = n; return this; }
        return this._name;
    }
}
 
let a = new A("abc");
 
console.log("name = " + a.name());
a.name("bbc");
console.log("name = " + a.name());
 
//EOF