In an object's prototype function, this refers to the object that is being called. In other scenarios, this might refer to different things (e.g execution context, or global/window if it's not set, or null if strict mode is also active). You can also specify the context when calling a function by using Function.prototype.call/apply/bind.
I assume 'this' refers to a specific array that the numberOfOccurence method is applied to. However, can someone explain how 'this' works in this code in more detail? Thanks!
In JavaScrpt,
this
is the execution context:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work
In an object's prototype function,
this
refers to the object that is being called. In other scenarios,this
might refer to different things (e.g execution context, orglobal/window
if it's not set, ornull
if strict mode is also active). You can also specify the context when calling a function by usingFunction.prototype.call/apply/bind
.I assume 'this' refers to a specific array that the numberOfOccurence method is applied to. However, can someone explain how 'this' works in this code in more detail? Thanks!
[3, 2, 3, 7, 7, 0].numberOfOccurances(3) // returns 2
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Can someone explain what's happening here?
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution