Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 19th, 2003, 01:24 PM
Authorized User
Join Date: Jun 2003
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 ';

Can anyone tell me if this is possible? Thanks.
Old July 20th, 2003, 09:41 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

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:
function trim(this)
  //code here
i.e. the first parameter is the instance of the class that called the function.



Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 02:25 AM
Functions LTello BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 September 15th, 2006 11:15 AM
Functions czambran BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 13 July 11th, 2006 06: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 03:47 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.