473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Prototype not working:

OK I have a script, designed to test my handling of named-parameters. That
is, I want to be able to call a Function as such:

function(val1, val2, val3); // order-based parameters
or
function({arg1: val1, arg2: val2, arg3: val3}); // named
parameters

and create a new method on the Function object to detect whether the user
used order-based or named paramters.

I have the method created, and it works if I define it like this:

function getArgs(function, argsExpected, argsPassed, defaults);

But not if I define it like this:

Function.prototype.getArgs(argsExpected, argsPassed, defaults);

I get a javascript error saying "this.getArgs is not a function".

I'd like to use "prototype" since that way it encapsulates "getArgs" in the
Function class.

Here is my code:

Function.prototype.getArgs = function(argsExpected, argsPassed,
arrDefaults) {
// some code to detect whether a function is passed named paramters
or order-based arguments
// argsExpected is an array of argument names
// argsPassed is always the "arguments" collection
// argsDefaults is an optional array of defaults for each arg in
argsExpected
// Sets this[i] to the values passed in for each parameter i.
}

function BodyStyle() {
this.getArgs(['style', 'doors'], arguments, ['coup', 2]);
}

function Car() {
this.getArgs(['bodyStyle', 'make', 'model'], arguments, [undefined,
'Mazda', 'Miata']);
}

var sedan = new BodyStyle('sedan', 4);
var coup = new BodyStyle('coup', 2);
var wagon = new BodyStyle('wagon', 5);

var car1 = new Car(sedan, 'Toyota', 'Camry');

Any suggestions?
Thanks,
Jamie

Jul 20 '05 #1
1 1905
> I have the method created, and it works if I define it like this:

function getArgs(function, argsExpected, argsPassed, defaults);

But not if I define it like this:

Function.prototype.getArgs(argsExpected, argsPassed, defaults); function BodyStyle() {
this.getArgs(['style', 'doors'], arguments, ['coup', 2]);
}

The result is a method of functions. But you invoke it as a method of the
object, which of course is wrong. The method is not the object. Think of the
correspondence between object.method and this == object.

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Catherine Jo Morgan | last post by:
I get the point of backing up the database I'm working on, but what's a prototype copy? How do I make one and why is it important to work on that rather than on the main database? Or is this more...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great...
2
by: hardrock | last post by:
Hello! I'm working with the prototype library version 1.4.0 and having a strange error lately. When I want to make an Ajax.Updater call, it basically works. But as soon as I put the call into...
6
by: libsfan01 | last post by:
Hi all Im trying to use prototype for an xmlhttprequest, but it doesn't seem to be working cross-browser. Is there someway of getting it to be IE6 compatible (active x)? here's my code so...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
37
by: nolo contendere | last post by:
I've recently begun playing with prototype.js as well as scriptaculous, and found both very helpful in developing web pages with lots of pop. Modal boxes, SlideUp/Down, calendars, etc. I've also...
5
RamananKalirajan
by: RamananKalirajan | last post by:
Hi guys I am facing this problem very often I dont know the reason. Can any tell this why. My Problem is, I am jsut new to the prototype. When I made a prototype file like this <html> <head>...
6
RamananKalirajan
by: RamananKalirajan | last post by:
Hi I am working with prototype. I am just a begginer. I tried to create a table dynamically with DOM in prototype. The code is working in Mozilla but not in IE. Any suggestions. <html> <body>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.