I am trying to push copied text into an empty array initialized by the constructor, but when the method is called, the error Cannot read property 'push' of undefined is shown. I am new to OOP and really want to start structuring my code as objects, but I always hit a snag and give up.
I have tried to modify the structure of my class by declaring the public field before the constructor and logging data to the console
When I log copyText() method to the console, I want to see the data inserted to create methods to modify/access it.
class CopyRecord {
// storedText;
constructor() {
this.storedText = [];
}
copyText() {
document.addEventListener('copy', function() {
let selectedText = window.getSelection().toString();
console.log(selectedText);
this.storedText.push(selectedText);
return selectedText;
})
}
}
let copy = new CopyRecord();
console.log(copy.copyText());