3

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?

  • With the keyword `new` you are creating a new object which you can reference using the variable `john`. Remove `new` and you store the result of a call to `Person` in `john`. As simple as that. W3schools has a good tutorial for beginners. – nbro Sep 25 '15 at 07:09
  • Variable declarations are processed before any code is executed, so *john* is created and assigned a value of *undefined* before any code runs. Similarly, function declarations are also processed before execution, so *Person* also exists before execution starts. When execution runs, *john* is assigned the return value of the expression on the right, which is an instance of *Person*. – RobG Sep 25 '15 at 07:09
  • 2
    @Rinzler—please show where w3schools explains how an execution context is created and environment record established. I don't think the OP will learn that there. – RobG Sep 25 '15 at 07:14
  • 1
    Why the downvote? The question can seem naive, but it is clear and well written – Pablo Lozano Sep 25 '15 at 07:19
  • @RobG I said: Allah Akbar has a good tutorial for beginners, I didn't say anything else. – nbro Sep 25 '15 at 07:44
  • @Rinzler—sure, it's a comment. But w3schools is not a particularly good site for learning javascript (it doesn't have anything that addresses the OP), MDN and the language specification are better resources. – RobG Sep 25 '15 at 08:35

4 Answers4

1

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)

TrapII
  • 2,219
  • 1
  • 15
  • 15
  • 2
    To add to this: if you see `new someFunction()` the `()` indicates it's being executed at that specific moment and the `new` means that instead of using the `return` value, it will create a new instance of that function. – Stephan Bijzitter Sep 25 '15 at 07:26
1
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().
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

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

Community
  • 1
  • 1
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0

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.

ralh
  • 2,514
  • 1
  • 13
  • 19