3 Replies - 528 Views - Last Post: 28 May 2018 - 09:38 AM

#1 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-January 17

Instantiating a new object - Which way and why?

Posted 28 May 2018 - 08:51 AM

Hey,

There are 5 different ways to create a new object. These are the first 3 ways.
let myObject = {};

var myObject = {
    name: "Engage",
    days: 30
}


var myObject = new Object();


Now my question is about the last 2 ways to create a new object.

You can either use an existing object as prototype of your new object, like this:

var person = {
  name: "David",
  age: 20,
};

// Create customStyle object and use letterSyle object as its prototype
var theDude = Object.create(person);




Or you can use a Constructor function to create your new object like this:

function Person (name) {
    this.name = name;
    this.age = 20;
    this.getInfo = function getInfo() {
        return this.name + ' is ' + this.age + ' years old.';
    };
}
 
// Instantiating a new object using the new keyword and a constructor function
var theDude = new Person("David");





My question is:

What is the difference between the last 2 methods of creating a new object?
Which method is more recommended?
In the real world, which one is used more?

Thanks

This post has been edited by Dormilich: 28 May 2018 - 08:59 AM
Reason for edit:: fixed quotes


Is This A Good Question/Topic? 0
  • +

Replies To: Instantiating a new object - Which way and why?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Instantiating a new object - Which way and why?

Posted 28 May 2018 - 09:09 AM

Quote

What is the difference between the last 2 methods of creating a new object?

Object.create() lets you fine tune object meta properties (e.g. if a property should turn up in for..in).

Mind that the way you have written the code with Object.create() is not how you should assign properties. The first parameter defines the prototype (shared properties across object). You can see that with Object.keys().

Quote

Which method is more recommended?

That strongly depends on who you ask.

People with a more functional background tend to go for the factory, while people with a class-based background prefer new.

Quote

In the real world, which one is used more?

Neither of them. Usually you use the second way you mentioned before that.
Was This Post Helpful? 1
  • +
  • -

#3 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-January 17

Re: Instantiating a new object - Which way and why?

Posted 28 May 2018 - 09:27 AM

Thanks

You wrote:

Quote

Neither of them. Usually you use the second way you mentioned before that.

But what if I want to instantiate a new object from an existing one? Isn't it that in this case I have to choose between one of the last two methods?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Instantiating a new object - Which way and why?

Posted 28 May 2018 - 09:38 AM

View Postkayut, on 28 May 2018 - 06:27 PM, said:

But what if I want to instantiate a new object from an existing one?

That would be cloning an object.

View Postkayut, on 28 May 2018 - 06:27 PM, said:

Isn't it that in this case I have to choose between one of the last two methods?

Nope. you would likely use a mixin.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1