I'm trying to learn Object.create by making a contrived calculator module. I've tried bind I've tried removing this, but no results.
QUESTION:
How do you reference a property of an object within another property of an element like you would with a class. Or is my example not a very good pattern? If so, how should I structure my Calculator Object to afford event listeners on creation?
Calculator.js
const Calculator = {
inputArr: [],
init: (selector)=> {
const el = document.querySelector(selector);
el.addEventListener('click', this.pushValue); // this wont work.
return this;
},
pushValue: (e) => {
let val = e.target.value;
if(val){
this.inputArr.push(val);
console.log(e.target, this.inputArr); // this wouldn't work.
}
}
};
const adder = Object.create(Calculator).init('#calc');
HTML:
<div id="calc">
<button class="btns" value="1">1</button>
<button class="btns" value="2">2</button>
</div>
The problem in that code is you've used arrow functions, but closing over the wrong
this. Arrow functions close over thethiswhere they're defined, rather than having it set when they're called. In your case, it's closing over thethisat global scope.If you make
initandpushValuenormal functions and call them via references to the object created viaObject.create, they'll get called with the correctthis:You do need to
bindthe call topushValuefrom the event listener (otherwise,thiswill refer to the element). Or alternately, wrap it in an arrow:Working example using the arrow wrapper on
this.pushValue: