Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // the class definition
- const $class = {
- hello() {
- console.log(`hello, my name is ${this.name}`);
- },
- };
- // this function will create new $class instances
- function createClass(name) {
- const instance = {
- name,
- };
- return Object.setPrototypeOf(instance, $class);
- }
- // we don't line newing things, as it's not necesary
- // but if you do, do it from inside a factory function
- // eg. (data) => new Class(data)
- // that way, your code is cleaner, SOLID and everybody is happy!
- const annie = createClass("annie");
- const jack = createClass("jack");
- annie.hello();
- jack.hello();
- console.log(`are annie and jack the same object?`, annie === jack); // false, two different objects
- console.log(`is any insance of class?`, annie instanceof createClass); // false, because we don't use the new operator
- // true, good!
- console.log(
- `is jack's prototype $class?`,
- Object.getPrototypeOf(jack) === $class,
- );
- // true, awesome!!
- console.log(
- `do annie and jack prototype is the same?`,
- Object.getPrototypeOf(annie) === Object.getPrototypeOf(jack),
- );
Add Comment
Please, Sign In to add comment