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>