I have a function that I am calling the following way but it returns someVariable.clean is not a function
Here is the code:
var someVariable = function() {
this.clean = function(obj) {
alert(obj);
}
}
someVariable.clean('test'); //call
Any idea why it is happening, what am I doing wrong?
If your’re not in ES5 strict mode, you’ll add the function
.clean()to the global object.So, just calling
clean('test');would indeed work here. If you want it like you described, you need to return the function an object.If you are in ES5 strict mode, this code would throw an error, since
thiswould be bound tonull. What thethis context variableis referenced to, always depends on how the function is invoked. In your case, as described,thisis eitherwindowornull.It would also work, if you’d invoke your function with the
newkeyboard:That is because,
newmakes the function a constructor function andthisis always bound to a newly created object within the function.