Last active
June 20, 2021 14:46
-
-
Save ashwinkumar2438/cbd9e90f43a9d52618401e456bf277ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Matrix=(id)=>{ | |
const STATE={ | |
human_id:id, | |
current:'IDLING', | |
loaders:[] | |
} | |
const drive=()=>{ | |
STATE.loaders.push('driving'); | |
STATE.current='DRIVING'; | |
} | |
const work=()=>{ | |
STATE.loaders.push('working'); | |
STATE.current='WORKING' | |
} | |
const getActivity=()=>(STATE); | |
return {work, drive, getActivity} | |
} | |
const getHumanConstructor=()=>{ | |
let id=0; | |
let matrix={}; | |
return class Human{ | |
constructor(name){ | |
this.name=name; | |
Object.defineProperty(this,'id',{ | |
value:++id, | |
writable:false, | |
configurable:false, | |
enumerable:false, | |
}) | |
matrix[this.id]=Matrix(this.id); | |
} | |
drive(){ | |
matrix[this.id].drive(); | |
console.log(`${this.name} is driving.`) | |
} | |
work(){ | |
matrix[this.id].work(); | |
console.log(`${this.name} is working.`) | |
} | |
__checkActivity(){return matrix[this.id].getActivity()} | |
} | |
} | |
const Human=getHumanConstructor(); | |
var Neo=new Human('Neo'); | |
Neo.drive(); //@logs "Neo is driving." | |
Neo.work(); //@logs "Neo is working." | |
Neo.__checkActivity(); //@returns {human_id: 1, current: "WORKING", loaders: ["driving", "working"]} | |
var Trinity=new Human('Trinity'); | |
Trinity.drive(); //@logs "Trinity is driving." | |
Trinity.__checkActivity(); //@returns {human_id: 2, current: "DRIVING", loaders: ["driving"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment