Nathan Parker
Nathan Parker

Reputation: 41

Javascript - Function Being Executed Immediately when Stored as Variable?

First post. New to programming.

In Javascript, when I declare a new variable and also set this newly declared variable to store a function, is that function immediately executed when the code runs (execution context)?

For instance,

function Person() {

    console.log(this);
    this.firstname = 'John';
    this.lastname = 'Doe';

}

var john = new Person();

Is the variable john being declared at the same time the function "Person" is being executed on that same line?

Upvotes: 3

Views: 655

Answers (4)

Gaurav Gupta
Gaurav Gupta

Reputation: 4701

var john = new Person();
  1. Variable john is created.
  2. RHS is executed.

    2.1. new operator calls the contructor of the Person. Every function implictly has a constructor(). This results in Object creation.

  3. Result of RHS evaluation is returned (which is an object in this case).

  4. Result is assigned to john or in other words, now john (a reference variable) will refer to the object which was created as a result of new operator executed on Person().

Upvotes: 1

TrapII
TrapII

Reputation: 2275

What you copy/paste is an object creation. It means john is a new Person object. Person being the class of the object. The this keywork in your Person function is related to the instance of the newly created object. Don't be abused by the function keyword, which is a confusing (because of historical reason) JS keyword. Person function can be seen as an object constructor.

EDIT : removed the off topic remark (really too much off topic)

Upvotes: 1

Pablo Lozano
Pablo Lozano

Reputation: 10342

In Javascript, variable definitions are "hoisted". It means that the code that you posted and the following one...

var john;
function Person() {
    console.log(this);
    this.firstname = 'John';
    this.lastname = 'Doe';
}
john = new Person();

... are identical, because the compiler will hoist the variable declaration to the beginning of the current context. So, the answer is no, the variable is declared, then the function is executed as a constructor, and then the new creadted object is assigned to that variable.

You can find more information here

Upvotes: 0

ralh
ralh

Reputation: 2564

If you put () or (optionalArgumentsHere) (some functions take arguments, some don't) after a function it means it is a function call, and it will be executed. If you want to assign the function itself to a variable, you need to omit the (). It is possible because functions are objects in JavaScript (which is not true in every language).

So what happens here is that you declare a variable (you actually declare it earlier because of hoisting, as explained by Pablo), you execute new Person() and assign the result to the variable.

If you called some function like

function fun() {
    return 5;
}

var x = fun();

You would be assigning the return value of the function to the variable. But your case is special, because you use new. That means Person is a constructor, and it is used to create a new object of type Person. Inside that constructor, you use the this keyword to attach properties to the newly created object. The new Person() call returns the object, even though return is not called explicitly.

Upvotes: 0

Related Questions