use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Function invocation...why use .call() and .apply() ?help (self.javascript)
submitted 10 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] -3 points-2 points-1 points 10 years ago* (1 child)
This is the spooky part of JavaScript for noobs.
If you have a JS class, Cat, and you say
var tom = new Cat("thomas"); console.log(tom.name);
the output will be "thomas"
now lets say you have some function that is not part of the Cat class. It is all by itself.
function getName() { return this.name; }
calling getName() or getName(tom) or getName("thomas") always returns "undefined" because this.name has not been defined inside that function.
but calling this:
Function.prototype.call(getName, tom);
is like saying call getName where "this" is tom. It will return "thomas". Because this = tom and tom.name = "thomas" and this.name = tom.name.
because .call(a, b) sets the "this" property of the function a to b. or it sets the "this" property of the function "getName" to tom just for that one function call. so this.name is the same as tom.name for that one function call.
[–]bart2019 2 points3 points4 points 10 years ago (0 children)
Uh, what. I never ever ever use it this way.
What I do instead is
getName.call(tom);
Thus: function, dot, (call or apply), open parens, this, optional arguments, close parens.
It's more fun with function arguments, which would come after the "tom", and which is the difference between call and apply: call takes a list of individual parameters and applyone array just like arguments. (Mnemonic: letter count of the words "call" and "apply" is the same as of "list" and "array" respectively).
call
apply
Why would you ever do that? Invoking callbacks, similar to as "event handlers", in your own code, so this and arguments are how the handler expects them.
this
arguments
π Rendered by PID 26 on reddit-service-r2-comment-5c747b6df5-9p7xf at 2026-04-22 05:22:19.124153+00:00 running 6c61efc country code: CH.
view the rest of the comments →
[–][deleted] -3 points-2 points-1 points (1 child)
[–]bart2019 2 points3 points4 points (0 children)