please dont rip this site
Microsoft® JScript
Creating Your Own Objects
| Tutorial |


Before you can create instances of an object, you must first define it by giving it properties and/or methods. The following example defines a pasta object. Notice the keyword this, which you use to refer to the current object.


function pasta( grain, grain2, width, shape, shapenum, extent, egg ) //  Define a "pasta" object.
{      
        this.length = 7;   //  Number of properties in the object, not including this one.
        this.grain = grain;   //  What grain is it made of? (string)
        this.grain2 = grain2;   //  Any other flour in it? (string)
        this.width = width;     //  How wide is it? (number, to be given in centimeters)
        this.shape = shape;   //  What is the cross-section? (string)
        this.shapenum = shapenum;  //  Is it one of the registered shapes? (number)
        this.extent = extent;    //  How long is it? (number, to be given in centimeters)
        this.egg = egg;  //  Does it have egg yolk in it as a binder? (Boolean)
}
Once you've defined an object, you create instances of it with the new statement.


var spaghetti = new pasta("wheat", "", 0.2, "circle", 9, 30, true);
var linguine = new pasta("wheat", "", 0.3, "oval", 17, 30, true);
You can add properties to one instance of an object, to change that instance. Those properties do not become part of the definition of the object, and do not show up in other instances unless you specifically add them. If you want the extra properties to show up in all instances of the object, you must add them to the definition.


//  Additional properties for spaghetti:
spaghetti.color = "pale straw";
spaghetti.drycook = 7;
spaghetti.freshcook = 0.5;   //  The spaghetti object has three more properties now.

var chowFun = new pasta("rice", "", 3, "flat", , 12, false); 
/*
Neither the chow fun object, the linguine object, nor the pasta object definition
has the three extra properties given to the spaghetti object.
*/

Including Functions in the Definition

It is possible to include functions in the definition of an object. The functions are then methods of the object. The following example builds an object that consists of an array of strings, and a method. The method adds a string to the array, increasing its size in order to do so. Notice that this makes each instance of the object indefinitely extensible.


function addItem(newItem)  //  Define a function to extend the list.
{
    this.length += 1;  //  Increment the length of the array.
    this[(this.length-1)] = newItem;  //  Add the new item, maintaining item numbering.
}

function shoppingList(firstItem) //  Define a "shopping list" object, with one item on the list.
{
    this.length = 2;  //  Number of properties in the object, not including this one.
    this.addItem = addItem;   //  Include the extend function as a method.
    this[(this.length-1)] = firstItem;  //  The first item is numbered 1.
}

var myList = new shoppingList("Milk");
myList.addItem("Eggs");  //  Use the method to add Eggs, which become item 2.
myList.addItem("Breadfruit");  // Breadfruit becomes item 3.
At this point, the contents of the array are as follows:


myList[length] is 4
myList[addItem] is the addItem function
myList[1] is Milk
myList[2] is Eggs
myList[3] is Breadfruit

Note that the indexing is not exactly as you might expect it to be if it were handled in a strictly numeric way. If you execute a for...in loop on this array, the loop iterates in the order given here, and the loop variable has the initial value "length" rather than 0.


© 1996 by Microsoft Corporation.


file: /Techref/language/asp/js/304.htm, 4KB, , updated: 1996/11/22 11:12, local time: 2024/5/23 05:19,
TOP NEW HELP FIND: 
34.239.158.223:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://piclist.com/techref/language/asp/js/304.htm"> Microsoft&#174; JScript Language Tutorial </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .