Object constructor in javascript

Object constructor in javascript
0

#1

Is this understanding of object constructors in javascript correct?

function Bird() {
  this.name = "Albert";
  this.color = "blue";
  this.numLegs = 2;
  // "this" inside the constructor always refers to the object being created
}

let blueBird = new Bird();
  1. Bird() is a function, that is also at the same time, a object constructor.
  2. After let blueBird = new Bird() then the real object has been created, which is blueBird
  3. So right now, object blueBird is:
let blueBird = {
 name: "Albert",
color: "blue",
numLegs:"2"
}

edit:
4. then the newly created object blueBird is an instance of object constructor Bird() correct?

can one object constructor has multiple instances? they can right? every time you pass in arguments into object constructors then more and more instances (newly created objects) are gonna come out right?


#2

The point of it is if you want to create a load of objects that have the same properties. So yup, the function lets you create multiple objects of that type of object. It is just a regular function, there is no difference to any other function except that you have to use new when you call it (otherwise it won’t return anything, and you’ll cause problems for yourself)


#3

@RandellDawson :wink: