Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 19th, 2003, 02:24 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding prototype functions

This is hard to word, so bear with me. I want to know if it is possible to add a prototype function that will directly modify the calling variable's value. Confused? Here is an example

<SCRIPT language="JavaScript">
String.prototype.trim = trim;

function trim()
{
    // Code that trims the white space from the calling myVar variable here
    // The value of myVar is retrieved from "this"
    // Modifying "this" causes an error. I think it this is because "this" is a reference to the current context object and not the actual calling variable
}
var myVar = ' name ';
myVar.trim();
alert(myVar);
</SCRIPT>

Can anyone tell me if this is possible? Thanks.
Reply With Quote
  #2 (permalink)  
Old July 20th, 2003, 10:41 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I doubt it, JavaScript doesn't support pass by reference out parameters. When you call myVar.trim() you are in fact calling trim(myVar) and the function trim is written as:
Code:
function trim(this)
{
  //code here
}
i.e. the first parameter is the instance of the class that called the function.


--

Joe
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 03:25 AM
Functions LTello BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 September 15th, 2006 12:15 PM
Functions czambran BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 13 July 11th, 2006 07:33 PM
Functions Meg Intro Programming 1 January 23rd, 2006 01:03 PM
Adding descriptions to values in functions katsarosj VB.NET 5 August 17th, 2004 04:47 PM



All times are GMT -4. The time now is 06:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.