please dont rip this site
Microsoft® JScript™
prototype Property
 Language Reference 
Version 2 

See Also                  Applies To


Description
Contains a reference to the prototype for a class of objects.
Syntax
objectname.prototype

The objectname argument is the name of an object.

Remarks
Use the prototype property to provide a base set of functionality to a class of objects. New instances of an object "inherit" the behavior of the prototype assigned to that object.

For example, say you wanted to add a method to the Array object that returns the value of the largest element of the array. To do this, declare the function, add it to Array.prototype, and then use it.

function array_max( )
{
  var i, max = this[0];
  for (i = 1; i < this.length; i++)
  {
     if (max < this[i])
	     max = this[i];
  }
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
After this code is executed, y contains the largest value in the array x, or 6.

All intrinsic JScript objects have a prototype property that is read-only. Functionality may be added to the prototype, as in the example, but the object may not be assigned a different prototype.

This is not the case for user-defined objects: User-defined objects may be assigned a new prototype.

The method and property lists for each intrinsic object in this language reference indicate which ones are part of the object's prototype, and which are not.


© 1997 by Microsoft Corporation. All rights reserved.

+

More documentation and examples from Netscape. :

Example 1: Add a property to a user-defined object. The following example uses the function Car to define a Car object type. It then uses new to create myCar, an instance of the object. The code Car.prototype.wheels=4 adds the wheels property to all instances of the Car object.

function Car(make, model, year) {
   this.make = make
   this.model = model
   this.year = year
}

var myCar = new Car("Acura", "Integra", 1987)
Car.prototype.wheels = 4        // no 3-wheelers please!

if (myCar.wheels == 4)
   document.write("The car myCar has ", myCar.wheels, " wheels.")

Example 2: Add a method to String objects. The following example creates a method, str_rep, and uses the statement String.prototype.rep = str_rep to add the method to all String objects. All objects created with new String() then have that method, even objects already created. The example then creates an alternate method and adds that to one of the String objects using the statement s1.rep = fake_rep. The str_rep method of the remaining String objects is not altered.

var s1 = new String("a")
var s2 = new String("b")
var s3 = new String("c")

// Create a repeat-string-N-times method for all String objects
function str_rep(n) {
var s = "", t = this.toString()
while (--n >= 0) s += t
return s
}
String.prototype.rep = str_rep

// Display the results
document.write("<P>s1.rep(3) is " + s1.rep(3))     // "aaa"
document.write("<BR>s2.rep(5) is " + s2.rep(5))    // "bbbbb"
document.write("<BR>s3.rep(2) is " + s3.rep(2))    // "cc"

// Create an alternate method and assign it to only one String variable
function fake_rep(n) {
return "repeat " + this + n + " times."
}

s1.rep = fake_rep

document.write("<P>s1.rep(1) is " + s1.rep(1))    // "repeat a 1 times."
document.write("<BR>s2.rep(4) is " + s2.rep(4))   // "bbbb"
document.write("<BR>s3.rep(6) is " + s3.rep(6))            // "cccccc"

This example produces the following output:

s1.rep(3) is aaa
s2.rep(5) is bbbbb
s3.rep(2) is cc

s1.rep(1) is repeat a1 times.
s2.rep(4) is bbbb
s3.rep(6) is cccccc

The function in this example also works on String objects not created with the String() constructor. The following code returns "zzz".

"z".rep(3)
+


file: /Techref/inet/iis/jscript/htm/js551.htm, 6KB, , updated: 2008/1/31 04:22, local time: 2024/11/25 12:08,
TOP NEW HELP FIND: 
18.218.99.80: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://sxlist.com/techref/inet/iis/jscript/htm/js551.htm"> prototype Property</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 sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .