edit-article
Home
Up
Delete
Article Name:
Article Description:
002-CREATING-objects-using-an-object-constructor-function - creating "object" instances by calling an object Constructor function.
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">creating objects using an object constructor function</h1> <h2 style="text-align: left;">[what]</h2> <ol> <li>[] CREATING-objects-using-a-constructor-function - creating an "object" instance or instances by calling an object Constructor function.</li> <li>[] A "object" constructor function is a function whose purpose it is, is to create a new instance of a given object.</li> <li>[] Typically the constructor function will have parameters specified that we can use to pass the object property values into.</li> <li>[] Sometimes we get those values from user input, or a db qry or a file.</li> <li>[] So we create an object with property names only and then pass that object "variables" to assign "values" to the properties in the object. </li> </ol> <h2>[why]</h2> <ol> <li>[] when we dont know what values the properties of our object will contain when we define/create/declare the object,</li> <li>[] 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.</li> </ol> <h2>[where]</h2> <ol> <li>[]</li> </ol> <h2>[when]</h2> <ol> <li>[x] 2013-09-03 -</li> <li>[x] 2019-12-07 - updated</li> </ol> <h2>[example]</h2> <ol> <li><strong>[] CREATING AN OBJECT CONSTRUCTOR FUNCTION</strong></li> </ol> <p>function Person (id,name,birthdate){<br /> this.id = id;<br /> this.name = name;<br /> this.birthdate = birthdate;</p> <p>}</p> <ol> <li><strong>] CREATING an OBJECT INSTANCE using the OBJECT CONSTRUCTOR FUNCTION<br /></strong></li> </ol> <p>var myPerson = new Person(1, "john doe", "2000-01-01");</p> <h2>[how-to]</h2> <ol> <li><strong>[] creating a "Person" object constructor function<br /></strong></li> <ol> <li>[] defining the function that will create Person objects.</li> </ol> <li><strong>[] creating an instance of a Person object <br /></strong></li> <ol> <li>[] once you have created a <em>constructor function</em>, you can now use it to <em>create instances</em> of your objects</li> </ol></ol> <h2>[reference]</h2> <ol> <li>[x] # 7450 <a href="/view/task?id=7450" target="_blank">UPDATE-article# 1681</a> (this) -</li> </ol> <p> </p> <hr /> <p> </p> <p><br /> </p>