user
user

Reputation: 2555

How are JavaScript function objects different from Scala function objects?

In Scala a function with two parameters is of type:

class Function2[InputTypeParameter1,InputTypeParameter2, OutputTypeParameter] {
  def apply(x:InputTypeParameter1,y:InputTypeParameter2 ):OutputTypeParameter=f(x,y);
}

The Function### class has one method, apply. When a method is used as a function object it is changed into a function object that has an apply method.
Methods can't be Function### objects because if they were, they would have a method apply that would be a Function### object that would have a method apply that would ha...

Does JavaScript work in a similar way?

Upvotes: 0

Views: 73

Answers (1)

cbalos
cbalos

Reputation: 909

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:

  1. A javascript internal operation, PrepareForOrdinaryCall, is performed in order to take care of some more context related stuff.
  2. Another internal operation, OrdinaryCallBindThis happens... this sets up some more context related stuff and also the function's this to be used during execution.
  3. 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.

Upvotes: 1

Related Questions