What is a function in javascript
MDN describes functions nicely:
In JavaScript, functions are first-class objects, i.e. they are
objects and can be manipulated and passed around just like any other
object. Specifically, they are Function objects.
Functions are formed from the Function.prototype (whose root is Object.prototype).
How are functions interpreted in javascript
Function.prototype
includes a method Function.prototype.call()
which
calls (executes) a function and sets its this to the provided value,
arguments can be passed as they are.
You may have seen code where call()
is used explicitly in order to pass this
like so:
function speak(phrase) {
document.write(phrase + ' ' + this.name);
}
function foo() {
this.name = "Foo";
}
speak.call(foo, "Hello");
Well, internally Javascript uses the Function.prototype.call()
method for all function/method calls. So
function speak(phrase) {
document.write(phrase);
}
speak("Hello");
actually is interpreted as:
function speak(phrase) {
document.write(phrase);
}
speak.call(window, "Hello");
When using strict mode, according to ECMAScript 5, there is a slight change:
speak.call(undefined, "Hello");
note that instead of passing window
as the this
argument, it passed undefined
.
Finally, when using a function as a sort of "method" for a object:
var speaker = {
speak: function(phrase) {
document.write(phrase);
}
}
speaker.speak("Hello");
becomes
var speaker = {
speak: function(phrase) {
document.write(phrase);
}
}
speaker.speak.call(speaker, "Hello");
How javascript functions compare to Scala functions/methods
While I do not know Scala, I did look up a little about the apply
method, and it sounds somewhat similar to call()
. Also according to this other SO answer
Under the hood, there are other differences between functions and
methods. Generally, a plain method generated less overhead than a
function (which technically is an object with an apply method).
Thus, I would say that a javascript function is very similar to a Scala function object. But methods in javascript are really just functions which are really just objects, so javascript methods are not like Scala methods. Hopefully this answers your question.
Update: How does javascript's call()
method work
call()
is an abstracted operation that uses the a function object's internal method [[call]]
. The internal [[call]]
method takes us into the nitty gritty of javascript's code execution. I will just scrape the surface of what happens:
- A javascript internal operation, PrepareForOrdinaryCall, is performed in order to take care of some more context related stuff.
- Another internal operation, OrdinaryCallBindThis happens... this sets up some more context related stuff and also the function's
this
to be used during execution.
- A third internal operation, OrdinaryCallEvaluateBody, is performed. This is where the function body is actually parsed, interpreted, and executed.
If you want to really read deep into this, all of everything I just said can be found in the EMCAScript Language Specification.