Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fillano
Created June 8, 2017 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fillano/4a3f9a744cb8f8edd0028fd1b976f5b2 to your computer and use it in GitHub Desktop.
Save fillano/4a3f9a744cb8f8edd0028fd1b976f5b2 to your computer and use it in GitHub Desktop.
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