简体   繁体   English

JavaScript Object创建方法有什么区别?

[英]What is the difference between JavaScript Object creation methods?

I've been trying to learn OOP in Javascript more deeply.There're different ways of creating class and objects in JavaScript. 我一直在尝试更深入地学习Javascript中的OOP。在JavaScript中创建类和对象有不同的方法。 If I understood correctly, two most popular ways are the ones below. 如果我理解正确,下面两种最流行的方式。 But the thing I don't understand what is the different between them. 但我不明白他们之间有什么不同。 The methods are giving exactly the same result. 这些方法给出了完全相同的结果。 If they are identical then why there are two different ways? 如果它们完全相同,为什么有两种不同的方式呢?

V1 V1

function Country(name){
    this.name=name;
    this.cities=[];
    this.continent;
}

Country.prototype={
    constructor:Country,
    addCity:function(name){
        this.cities.push(name)
    },
    setContinent:function(continent){
        this.continent=continent;
    }
}

V2 V2

function Country(name){
    this.name=name;
    this.cities=[];
    this.continent;

    this.addCity=function(name){
        this.cities.push(name);
    }

    this.setContinent=function(continent){
        this.continent=continent;
    }
}

Thank your four great answers. 谢谢你的四个好答案。 I understood the difference correctly. 我理解正确的区别。 Probably you know, it's been possible to create class and objects like in Java as of EcmaScript6. 也许你知道,从EcmaScript6开始,可以像Java一样创建类和对象。

Addition 加成

Then this system is identical to prototype method and there is no drawback to use. 然后该系统与原型方法相同,并且没有使用的缺点。

class Country
{

    constructor(name){
        this.name=name;
        this.cities=[];
        this.continent;
    }

    addCity(name){
        this.cities.push(name);
    }

    setContinent(continent){
        this.continent=continent;
    }
}

c1 = new Country()
c2 = new Country()
console.log(c1.addCity == c2.addCity) // gives true

I've tried @vothaison's method and like I said I guess this is the same as the prototype method. 我已经尝试了@ vothaison的方法,就像我说的那样,我猜这与原型方法相同。

Your two ways are not the same, and V1 is the way to go. 你的两种方式是不一样的,V1是要走的路。

With V1, all new instances of Country created will use the same innstace of addCity method and setContinent method. 使用V1,创建的Country的所有新实例将使用相同的addCity方法和setContinent方法。

Whereas in V2, all instances have their own instance of addCity method and setContinent method, which is a waste of resource. 而在V2中,所有实例都有自己的addCity方法实例和setContinent方法,这是浪费资源。

You test them with this code: 您使用以下代码测试它们:

c1 = new Country()
c2 = new Country()
c1.addCity == c2.addCity // true in V1, false in V2

V1 is the recommended way to go. V1是推荐的方式。

It uses the Prototype Pattern 它使用原型模式

The Prototype Pattern creates new objects, but rather than creating non-initialized objects it returns objects that are initialized with values it copied from a prototype - or sample - object. Prototype Pattern创建新对象,但不是创建非初始化对象,而是返回使用从原型(或样本)对象复制的值初始化的对象。 The Prototype pattern is also referred to as the Properties pattern. Prototype模式也称为Properties模式。

MDN explains the pros and cons very well: Inheritance and the prototype chain MDN很好地解释了优点和缺点: 继承和原型链

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

相关问题 用Javascript定义对象方法的两种方式有什么区别? - What is the difference between two ways of defining object methods in Javascript? 返回带有方法的对象的函数和带有方法的对象的变量之间有什么区别 - what is the difference between a function that returns an object with methods and a variable that is an object with methods 这些不同的JavaScript对象创建方法的正确应用是什么 - What are the proper applications of these different methods of object creation in JavaScript JavaScript中两个对象之间的区别是什么 - what is difference between two object in JavaScript JavaScript对象和基元类型有什么区别? - What is the difference between JavaScript object and primitive types? JavaScript中的“节点”和“对象”有什么区别? - What's the difference between “node” and “object” in JavaScript? 在javascript中,对象和命名空间之间有什么区别? - In javascript, what is the difference between an object and a namespace? 在 javascript 中导出 function 或 object 有什么区别? - What is the difference between exporting a function or an object in javascript? 将 JavaScript 代码放入<a>.</a> - What is the difference between the different methods of putting JavaScript code in an <a>? 这两种导入javascript模块的方法有什么区别? - What is the difference between these two methods for importing a javascript module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM