0

I'm doing a codeacademy.com lesson, specifically: "Javascript > Introduction to Objects I > Custom Constructors (21/33)"

In this lesson we're learning how to create constructors, like so:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

var bob = new Person("Bob", 27);

My question is, how can the constructor exist later on without first being placed into a variable? I'm very, very new to javascript but it was my understanding that unless you stored something in a variable it couldn't persist.

Is this like, a class declaration? Maybe this is only possible with Classes?

Hoping this question might shed some light in the dark on my understanding of javascript syntax. Thanks.

boulder_ruby
  • 33,076
  • 8
  • 66
  • 91

2 Answers2

1

Well, it kind of is being placed in a variable.

function x(){}

is more or less the same as

var x = function(){} 

So in your case, you do end up with a symbol called Person that points at your function/constructor.

Community
  • 1
  • 1
Thilo
  • 241,635
  • 91
  • 474
  • 626
1

If the function is never assigned anywhere and never called, the compiler's dead code detection will probably remove the function before it is evaluated, so it won't even be created in the first place. Only if the function is needed, it will be created and stored in memory somewhere. But only when it is called, an actual variable unstored is created in the new scope, so that it is accessible by variable inside there.

Bergi

(function (name, age) {
  this.name = name;
  this.age = age;
})

would not be stored.

BBS
  • 1,201
  • 2
  • 11
  • 23
  • This would not only not be stored anywhere, it would be a syntax error. – Bergi May 11 '15 at 01:41
  • Yes, but my point was if your using it as an anon function it does not get stored. – BBS May 11 '15 at 01:53
  • In a place where this construct was valid, it would *not* get stored once you named it. – Bergi May 11 '15 at 01:54
  • Yes, I am giving an instance of where it is not stored. – BBS May 11 '15 at 01:56
  • Yes, but now "*Once you name the function it gets stored*" is wrong. – Bergi May 11 '15 at 01:59
  • Really? Can you elaborate, and maybe give an example? – BBS May 11 '15 at 02:00
  • Isn't unstored still stored within the scope of the parens? – BBS May 11 '15 at 02:03
  • Oh I didn't see the link. – BBS May 11 '15 at 02:04
  • Which ones? Yes, `unstored` is accessible within `{}`, but since it is never executed no variable will get created for it. – Bergi May 11 '15 at 02:05
  • Won't the compiler store a copy of the function in case it does need to be accessed? Perhaps I am confused as to the difference between accessible and stored. – BBS May 11 '15 at 02:10
  • 1
    If the function is never assigned anywhere and never called, the compiler's dead code detection will probably remove the function before it is evaluated, so it won't even be created in the first place. Only if the function is needed, it will be created and stored in memory somewhere. But only when it is called, an actual variable `unstored` is created in the new scope, so that it is accessible by variable inside there. – Bergi May 11 '15 at 02:19
  • Okay that makes sense. Thanks for that! Can I quote you in an updated response? – BBS May 11 '15 at 02:21
  • Sure, assuming the quote is properly attributed I can't deny it :-) – Bergi May 11 '15 at 02:23