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


To create instances of an object, you must first define it by giving it properties and, if appropriate, methods. For instance, 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 )
{
  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)
  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)
  this.egg = egg;  // Does it have egg yolk as a binder? (Boolean)
}
Once you've defined an object, you create instances of it with the new operator.

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, but 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 object definition.

// Additional properties for spaghetti.
spaghetti.color = "pale straw";
spaghetti.drycook = 7;
spaghetti.freshcook = 0.5;

var chowFun = new pasta("rice", "", 3, "flat", , 12, false); 
/*
Neither the chowFun object, the linguine object, nor the pasta object definition
has the three extra properties given to the spaghetti object.
*/
Including Methods in the Definition
It is possible to include methods in the definition of an 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.
{
    this.length = 2;  // Number of properties in the object, not including this one.
    this.addItem = addItem;   // Include the addItem 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: 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.


© 1997 by Microsoft Corporation. All rights reserved.


file: /Techref/language/jscript/js6.htm, 5KB, , updated: 1997/9/30 02:44, local time: 2024/12/27 18:10,
TOP NEW HELP FIND: 
3.145.80.247: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://ecomorder.com/techref/language/jscript/js6.htm"> Creating Your Own Objects</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?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .