Important alert: (current site time 7/15/2013 1:21:26 PM EDT)
 

VB icon

Add a method to an object

Email
Submitted on: 6/5/1998
By: Found on the Web 
Level: Not Given
User Rating: By 2 Users
Compatibility: JavaScript
Views: 6654
 
     How can I add a method to an object? There are two ways you add a method to an object (and it has to be an object), either by using prototype (below) or Or by adding a method to the object (2nd below). Found at http://www.irt.org
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Add a method to an object
' Description:How can I add a method to an object? There are two ways you add a method to an object (and it has to be an object), either by using prototype (below) or Or by adding a method to the object (2nd below).
Found at http://www.irt.org
' By: Found on the Web
'
' Inputs:
'
' Assumes:Note that both approaches will not work on older browsers.
'**************************************

using prototype: 
<SCRIPT LANGUAGE="JavaScript"><!--
// define the myString object
function myString(value) {
this.value = value;
}
// define the textfunc() method
function testfunc(value) {
this.value = value;
}
// set the prototype
myString.prototype.testfunc = testfunc;
// create an instance of a myString object
var mytext = new myString('this is a test');
// alert the value propert of the mytext myString object
alert(mytext.value)
// use the myString testfunc() method
mytext.testfunc('teststring');
// alert the value propert of the mytext myString object
alert(mytext.value)
//--></SCRIPT>
 
Or by adding a method to the object: 
<SCRIPT LANGUAGE="JavaScript"><!--
// define the myString object
function myString(value) {
this.value = value;
this.testfunc = testfunc;
}
// define the textfunc() method
function testfunc(value) {
this.value = value;
}
// create an instance of a myString object
var mytext = new myString('this is a test');
// alert the value propert of the mytext myString object
alert(mytext.value)
// use the myString testfunc() method
mytext.testfunc('teststring');
// alert the value propert of the mytext myString object
alert(mytext.value)
//--></SCRIPT>


Other 219 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.