简体   繁体   中英

Memory usage for JavaScript object literal notation vs. constructor functions

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(); 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM