Jump to content

The best way to instantiate a new object


kayut

Recommended Posts

Hey,

There are 3 different ways to create a new object:

let myObject = {};

 

var myObject = {
    name: “Engage",
    days: 30
}
var myObject = new Object();

Now my question is about the 2 different ways of instantiating 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 theDude object and use person 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 instantiating a new object?
Which method is more recommended?
In the real world, which one is used more?

Thanks

Edited by kayut
Link to comment
Share on other sites

Does it not depend on the problem that you are trying to solve?  The second example simply creates an object with two properties and assigns a value to each.  The third example creates a function that fixes the value of one of the properties, allows you to assign a value to the other property, and provides a means to recover the information about both properties via a function that you can assign to the newly created instance.

If I have properly understand your intention, rather than your words, a better comparison would be

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

and

function Person (name, age) {
    this.name = name;
    this.age = age;
}
 
var theDude = new Person("David", 20);

In which case you are left with the same question.  What is the intended purpose of creating the object?

In both cases you are creating an object that you are assigning to a variable called theDude.  Now, why would you prefer one over the other do you think?

Roddy

 

 

Edited by iwato
Link to comment
Share on other sites

The purpose of an object template, such as "Person" is for organizing code. In any part of your software, if you are given an object of type Person you know for sure that you can find a name and age property on it. If the object has no type then you don't know what properties it might have.

You would use an object template if your software uses the same kind of object a lot. 

If you're only using the object in one place then you can use the shorthand {} to define it.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...