edit-article
Home
Up
Delete
Article Name:
Article Description:
001-CREATING-objects-using-an-object-literal - declaring an object variable by literally specifying(writing) out the objects property names and values and methods. declaring the object instance variable name and assigning the object properties values,
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">object literals</h1> <h2 style="text-align: left;"><br />[what]</h2> <ol> <li>[] CREATING-objects-using-an-object-literal - declaring an object variable by literally specifying(writing) out the objects property names and values and methods. declaring the object instance variable name and assigning the object properties values,all in one fell swoop, while you are writing the application (aka design time)</li> <li>[] an "object literal" creates an object instance by literally specifying the objects name, properties, methods and values in the objects definition at design time</li> </ol> <h2>[why]</h2> <ol> <li>[] objects can be created and instantiated at design time when all of the objects properties are known to the application developer</li> </ol> <h2>[why not]</h2> <ol> <li>[] in most/a lot of cases you will not know the name and properties of your object instance, therefore you cant define the object's properties without knowing what these items are.</li> </ol> <h2><strong>[where]</strong></h2> <ol> <li>[] your javascript app</li> </ol> <h2>[when]</h2> <ol> <li>[] you know what the objects name and properties are, prior to you using them. in many cases you don't know what they are ahead of time, so you will likely want to use on object constructor function</li> </ol> <h2>[example]</h2> <p>var myCar = {</p> <p> id:1,<br /> name: "dons Car",<br /> getName: function(){alert(this.name);}<br />};</p> <h2>[how-to]</h2> <ol> <li>[] in the EXAMPLE above a variable named myCar has been declared</li> <li>[] myCar is an OBJECT where we have added 2 properties to the myCar object,</li> <li>[] the properties are 'name' and 'id'.</li> <li>[] we have assigned a value of 1 to the id property and a value of "dons Car" to the name property</li> <li>[] note the semi colon that seperates the property from the value that we have assigned to the property</li> <li>[] note the comma that seperates each of the elements of the object</li> <li>[] We have also added a single method named 'getName' to this object</li> </ol> <h2>[reference]</h2> <ol> <li>[] js-101</li> <li>[] creating objects using a constructor function</li> <li>? [] creating objects using new obj()</li> <li>[] creating objects using Object.Create(obj)</li> </ol> <p> </p>