简体   繁体   English

JavaScript对象文字表示法与构造函数的内存使用情况

[英]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) (或者这个概念不适用于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) (或者这个概念不适用于JavaScript)

No. person (lower case) is a simple object with three properties. person (小写)是具有三个属性的简单对象。 Person (capitalized) is a function object, which has an associated (blank) prototype object ( Person.prototype ). Person (大写)是一个功能对象,它具有一个关联的(空白)原型对象( 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 ; 要创建一个由person支持的新实例,可以使用Object.create to create a new instance via Person and backed by Person.prototype , you'd use new : 要通过Person创建一个新实例并得到Person.prototype支持,则可以使用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; johnperson ,其原型获得sayName mary gets sayName from Person.prototype , its prototype. mary 从其原型Person.prototype获取sayName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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