0

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?

user
  • 2,345
  • 3
  • 12
  • 14
  • Instead of **bold** to emphasise words, consider using *italics* instead. It's much easier to read. – RobG Nov 08 '15 at 23:27
  • No, javascript functions are not implicitly coerced to anything, and there's no sugar for calling them. You can only invoke callable objects (function objects), instead of everything with an `apply` method. – Bergi Nov 09 '15 at 02:18

1 Answers1

1

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.

Community
  • 1
  • 1
cbalos
  • 879
  • 1
  • 9
  • 19
  • Please don't mix "context" with "this". A function's *this* is just one parameter of its *execution context* that can be set to any object, or to any value in strict mode. It should simply be called *this* or *this parameter*. There is also different treatment of *this* with [*bind*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.bind) and [*arrow functions*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-arrow-function-definitions). – RobG Nov 08 '15 at 23:18
  • @RobG I changed the one occurrence of "context" where it was being mixed improperly, as I agree. I don't think it is important to this question to discuss the different treatment of this with bind and arrow functions, but you raise a good point for general knowledge. – cbalos Nov 08 '15 at 23:26
  • But if functions are objects with call method and call methods are functions, isn't an infinite loop formed? In Scala this is solved by having methods be different from functions. So functions can be objects with an apply method because methods are different from functions. So the question is how does JavaScript solve this? – user Nov 19 '15 at 18:38