"as far as I understand this function will have global scope, doesn't it?"
"Scope" is about what variables a function has access to (i.e., its own local variables, plus locals declared in any functions that it might be nested inside, plus global variables), and that has nothing to do with the value of this
. Scope depends on where the function declaration is, but this
depends on how the function is called.
"since .call(this)
is passing window
object to this closure"
We can't tell that just from the code shown. Presumably you mean that that block of code is not nested inside some other function, so then yes, this
would be window
. But if that code was ever pasted inside some other function then that this
might have some other value depending on how that other function was called.
"Does changing .call(this)
to just ()
change anything?"
If you use just ()
that will mean that inside the function this
will be window
. Whether that changes anything depends on whether the this
in .call(this)
was window
in the first place - as I already mentioned above, if that block were nested inside some other function this
could be something else. Having said that, for the code shown if this
was anything other than window
I think your code would break. Because you declare a function like this:
this.deleteKeyword = function(model) {...}
...and then inside the second click handler you call what I assume is meant to be the same function without using this
:
deleteKeyword($(this));
The only way that will work is if this
is window
and the deleteKeyword()
function is thus global. If this
were any other object the function would be a property of that other object and thus not accessible directly as deleteKeyword()
. But unless you specifically want deleteKeyword()
to be accessible from other code not shown there is no reason to create it as a property of this
at all. Just declare it with var
:
var deleteKeyword = function(...
...and then it will be accessible only inside that function and the ones nested in it such as your click handler. And the .call()
at the end of your function would be redundant since the function wouldn't ever actually use this
.
"so isn't it a better option to apply jQuery object (or maybe just empty object) to it to increase performance? Wouldn't it increase performance if this would point to local scope? Since window object is very slow."
Well as I already said, "scope" and the value of this
are unrelated concepts. I'm not sure why you are worrying about performance for that code. But if you were to apply jQuery or an empty object with .call(jQuery)
or .call({})
the code would not work as explained above.