creating objects using an object constructor function
[what]
- [] CREATING-objects-using-a-constructor-function - creating an "object" instance or instances by calling an object Constructor function.
- [] A "object" constructor function is a function whose purpose it is, is to create a new instance of a given object.
- [] Typically the constructor function will have parameters specified that we can use to pass the object property values into.
- [] Sometimes we get those values from user input, or a db qry or a file.
- [] So we create an object with property names only and then pass that object "variables" to assign "values" to the properties in the object.
[why]
- [] when we dont know what values the properties of our object will contain when we define/create/declare the object,
- [] As we (mostly) need to create objects dynamically, (meaning we don't always know what the values of our objects properties will be when we create the object at design time), having a constructor function enables us to create new object instances when/where we require them.
[where]
- []
[when]
- [x] 2013-09-03 -
- [x] 2019-12-07 - updated
[example]
- [] CREATING AN OBJECT CONSTRUCTOR FUNCTION
function Person (id,name,birthdate){
this.id = id;
this.name = name;
this.birthdate = birthdate;
}
- ] CREATING an OBJECT INSTANCE using the OBJECT CONSTRUCTOR FUNCTION
var myPerson = new Person(1, "john doe", "2000-01-01");
[how-to]
- [] creating a "Person" object constructor function
- [] defining the function that will create Person objects.
- [] creating an instance of a Person object
- [] once you have created a constructor function, you can now use it to create instances of your objects
[reference]
- [x] # 7450 UPDATE-article# 1681 (this) -