2

declaring an object using literal notation like this:

var person = {
    name: "",
    gender: "",
    age: 0
}

vs. a Constructor function like this:

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

First question:

When declared like this do they both take an equal amount of memory even though they're not 'instantiated' yet? (or does this concept not apply to JavaScript)

Second question:

Can both of these correctly be newed up as follows:

var john = new person(); 
2
  • 3
    Objects and constructor functions are two different things (conceptionally), and it doesn't make much sense to compare them. Basically in the first case you created a single object, whereas a constructor function allows you to create many objects dynamically. Commented Sep 23, 2014 at 14:09
  • I see where you're coming from now and have updated my answer accordingly. Commented Sep 23, 2014 at 14:21

1 Answer 1

8

To avoid confusion, let's use different names:

// An object we might use as a prototype
var person = {
    name: "",
    gender: "",
    age: 0
};

// A constructor function, note the capital P
var Person = function(name, gender, age)
{
    this.name = name;
    this.gender = gender;
    this.age = age;
};

When declared like this do they both take an equal amount of memory even though they're not 'instantiated' yet? (or does this concept not apply to JavaScript)

No. person (lower case) is a simple object with three properties. Person (capitalized) is a function object, which has an associated (blank) prototype object (Person.prototype). So in theory, the function will take up more memory than the object, because we have the function object, its associated code, and the simple object (its prototype).

It's not likely to matter, though. Function objects in and of themselves don't take up a lot of memory, that code is small, and blank objects (the prototype) take up very, very little. Presumably you're not going to have millions and millions of these, as (if I'm understanding the point of your question correctly) they're meant to be the basis of other objects.

Can both of these correctly be newed up as follows:

var john = new person(); 

Not literally, no. But you can create instances based on each of them. To create a new instance backed by person, you'd use Object.create; to create a new instance via Person and backed by Person.prototype, you'd use new:

// Using the `person`
var john = Object.create(person);
john.name = "John";
john.gender = "M";
john.age = 47;

// Using `Person` (the constructor function)
var mary = new Person("Mary", "F", 32);

This really only becomes interesting when at least one of the prototype properties isn't set by construction. That property might be anything, but let's take the common case: A function:

// An object we might use as a prototype
var person = {
    name: "",
    gender: "",
    age: 0,
    sayName: function() {
        console.log("My name is " + this.name);
    }
};

// A constructor function, note the capital P
var Person = function(name, gender, age)
{
    this.name = name;
    this.gender = gender;
    this.age = age;
};
Person.prototype.sayName = function() {
    console.log("My name is " + this.name);
};

Then:

// Using the `person`
var john = Object.create(person);
john.name = "John";
john.gender = "M";
john.age = 47;
john.sayName(); // "My name is John"

// Using `Person` (the constructor function)
var mary = new Person("Mary", "F", 32);
mary.sayName(); // "My name is Mary"

john gets sayName from person, its prototype; mary gets sayName from Person.prototype, its prototype.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for explaining the difference. Now you just need to mention that OP should not care about memory :-)
@T.J. Crowder Thanks that was extremely useful. Marking as correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.