0

For some reason, I'm not able to get any value from this object:

function ContextMenu(id){
    this.ID = id;
}
ContextMenu.prototype.attachTo = (element, v = false) => {
    var menuID = this.ID;
    console.log(menuID);//undefined!
    [...]
};

Any idea why? This code is a part of the preload file in electron.

Adel Sbeh
  • 3
  • 4

2 Answers2

0

You’re defining attachTo as an arrow function, which means this.ID is pointing to the global scope. It’s giving you the value of window.ID, which is undefined.

Using function(element, v=false){ /* the rest of your code goes here */ should solve your problem.

You can read more about it in this answer: Arrow Functions and This

daniloxxv
  • 817
  • 6
  • 10
-1

Never mind, you can solve this problem by doing this:

ContextMenu.prototype.attachTo = function(element, v = false){
    var menuID = this.ID;
    console.log(menuID);
    [...]
};
Adel Sbeh
  • 3
  • 4