user5041486
user5041486

Reputation:

Javascript Function Expressions

I have recently been trying to learn javascript and have a couple of questions.

When you create a function expression:

var greet = function(){
    console.log('Hi');
}

Is that creating a function object and having the variable "greet" point to that function object in memory?

My second question if you have a function like this:

function log(a){
    console.log(a);
}

Then you make a call to that function:

log(greet); //greet is the function expression declared above.

So I know that when a function object is created there are two properties that are given to the object. The name (if provided, otherwise anonymous) and a code property which stores the code contained inside the parentheses of the function. Now I am a little confused on where the parameter "a" in the log function gets attached to in a function object. Is "a" just another property of the function object log and it simply just points to the memory address of anything that is passed into the log function? Where in this case it was a function expression called greet. Any input would be appreciated. Thank you!

Upvotes: 1

Views: 89

Answers (1)

RobG
RobG

Reputation: 147453

When a function is called as in:

log(greet)

then the identifier log is resolved in the current execution context and, if not found, is searched along the scope chain, ending at the global execution context. If not found, an error is thrown.

In this case, log has been defined using a function declaration so it exists in the global scope so is found. Its value is checked to make sure it's callable (again, if not, an error is thrown) if it is, it's called.

In the call, the identifier greet is resolved and its value passed to the function. If greet can't be resolved (i.e. it doesn't exist on the scope chain), an error is thrown. In this case, it resolves to a reference to the function assigned to greet.

When log is executed, a new execution context is created. The function declaration for log defines a formal parameter a (in its formal parameter list), so a is created as a local variable for log. The values in the call are passed to identifiers in the formal parameter list in order, so the value of greet is assigned to a. Note that initialisation and creation of a new execution context occurs every time a function is called.

The same process is followed when calling:

console.log(a);

so that within console.log, the reference to greet is passed as the first parameter, so it now references the greet function.

The behaviour of console.log is entirely implementation dependent so the internals are unknown, but for functions most tend to just call the function's toString method.

It's a handy feature of ECMAScript that an arguments object is created of the arguments passed to functions, so the values passed are always available as numeric properties of the arguments object if there is no parameter for them to be assigned to. So console.log doesn't have to define any formal parameters, it can just loop over its arguments object and process the passed values in turn.

Upvotes: 3

Related Questions