please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Function.prototype Property

Name

Function.prototype Property---the prototype for a class of objects

Availability

Navigator 3.0, Internet Explorer 3.0

Synopsis

function.prototype

Description

The prototype property of a function is only useful when the function is used as a constructor function. It refers to an object, which is initially empty--i.e., has no properties or methods defined.

The prototype object serves as a prototype for an entire class of objects. This is because any objects created through a constructor function will "inherit" the properties and methods defined in that prototype. This "inheritance" is done in a way that does not consume any memory in the inheriting object. When you use a property or invoke a method in the context of an inheriting object, the object's own properties and methods are first checked. If the property or method you specified is not found there, JavaScript then checks the properties and methods of the prototype object. This means that when you add properties to a prototype object, those properties will immediately appear to have been added to any existing instances of the corresponding object type.

Note that prototypes are not shared across windows. If you add a method to the String.prototype object in one window, then all strings in that window will have access to this new method. Strings defined in other windows, however, will not see the new method.

In Navigator 3.0, a prototype object is not created for a constructor function until the constructor is used to create an object for the first time. Thus, in Navigator 3.0, you must create and discard a dummy object with a constructor function before you can assign values to the constructor's prototype object. This is not necessary in Internet Explorer 3.0.

The title of this page is "Function.prototype". Note that there are two possible meanings for this title. The first, and the intended one, is that this page documents the prototype property shared by all Function objects. But note that all constructor functions have a prototype property and Function() is a constructor function. Therefore, this page could also be documenting the prototype property of the Function() constructor. In a sense it is: you can add new properties or methods to all JavaScript Function objects by setting those properties and methods in the Function.prototype object.

Usage

Prototypes are one of the main techniques for defining the features of an object type or "class" in JavaScript. They are particularly useful in providing methods and constants for use with a whole class of objects. They are also useful for defining properties with default values which individual objects may optionally override.

The prototype object can also be used to add new properties or methods to many predefined JavaScript object types as well, as the example below shows.

Example

The following code shows how you could add a new method to the String object type by specifying the method on the prototype object:

// Define a function
function String_output(d) { d.write(this); }
// Set it as a method in the String.prototype object
String.prototype.output =index.html String_output;
// Use it like this
"Hello World!".output(document);
In Navigator 3.0, this code makes the new output() method available for all strings in the current window. In Internet Explorer 3.0, however, it only makes the method available for String objects, not primitive strings. This misfeature will be fixed in a future release.

As another example, consider the Window object. It has a top property that refers to the top-level web browser window. Suppose you wanted all objects in your JavaScript application to contain a reference to this top-level window. You could implement this with a single line of code:

Object.prototype.top =index.html self.top;

After executing this code, any JavaScript object (all current and future objects) that does not already have a top property will now have one, and it will refer to the top-level window self.top.

Bugs

Navigator 3.0 requires a constructor to be used once before anything can be assigned to its prototype object. Internet Explorer 3.0 does not allow primitive strings to use methods or properties defined in String.prototype. Similarly, Internet Explorer 3.0 does not support prototype objects for the Boolean, Number, and Function objects.

See Also

Chapter 7, Objects


Previous Home Next
Function.caller Book Index Function.toString()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/refp_146.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/11/24 13:34,
TOP NEW HELP FIND: 
18.191.192.109: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/java/script/definitive/refp_146.htm"> [Chapter 21] Reference: Function.prototype</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!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .