Skip to content

Instantly share code, notes, and snippets.

@fillano fillano/mod1.js
Created Jun 8, 2017

Embed
What would you like to do?
create class with readonly attribute.
const t = Symbol('ttt');
module.exports = class {
constructor(name) {
this[t] = name;
}
get name() {
return this[t];
}
sayHello() {
console.log(`Hello ${this.name}`);
}
}
const Greeting = require('./mod1');
let m1 = new Greeting('Fillano');
m1.sayHello();//show 'Hello Fillano'
console.log(m1.name);//show 'Fillano'
m1.name = 'Allen';
console.log(m1.name);//still show 'Fillano'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.