...

View Full Version : Passing parameters to prototypes



Skyzyx
02-26-2003, 02:09 AM
Is it possible to pass parameters to prototypes? Such as...



var myString="Hello World!";
document.write(myString.color('#FF0000'));


This would be a case of passing a value of red to the prototype script to display the text as red. Something like this?



String.prototype.color=function(theColor)
{
return this.style.color=theColor;
}


This example doesn't work, but I'd like to know how to do it, if it's possible.

joh6nn
02-26-2003, 02:12 AM
yeah, there's absolutely no difference between a prototyped property/method and any other property/method.

glenngv
02-26-2003, 02:58 AM
The String object already has a built-in methods for formatting text:

big, blink, bold, fixed, fontcolor, fontsize, italics, small, strike, sub and sup.

myString = new String("Formatting methods")
document.write ("<p>big() = " + myString.big())
document.write ("<br>blink() = " + myString.blink())
document.write ("<br>bold() = " + myString.bold())
document.write ("<br>fixed() = " + myString.fixed())
document.write ("<br>fontcolor("5") = " + myString.fontsize("5"))
document.write ("<br>small() = " + myString.small())
document.write ("<br>strike() = " + myString.strike())
document.write ("<br>sub() = " + myString.sub())
document.write ("<br>sup() = " + myString.sup())

Skyzyx
02-26-2003, 04:03 AM
Glenn, I'm aware of those styles... actually, I want to create custom ones based on the same ideas as those. I just need to know how to do it.

String.prototype.color=function(theColor) doesn't seem to work... or maybe it's just me... would this work?

this.style.color='#FF0000';

... or am I missing something...

joh6nn
02-26-2003, 04:59 AM
String.prototype.color=function(theColor)
{
return this.style.color=theColor;
}

1) doesn't return anything, because this.style.color = somecolor, does not return a value.
2) this.style.color does not exist; Stings don't have style properties.

Skyzyx
02-26-2003, 05:55 AM
joh6nn,

I appreciate the help, but you're simply confirming what I already know... this script doesn't work.

The next step is... how can I make it work?

glenngv
02-26-2003, 06:02 AM
String.prototype.color=function(theColor)
{
return '<span style="color:'+theColor+'">'+this+'</span>';
}

i've used span tag instead of the font tag which i believe is deprecated in XHTML.

but if you still want to use font tag:

return '<font color="'+theColor+'">'+this+'</font>';

ca_redwards
02-27-2003, 03:49 AM
By defining custom methods for the String and Array prototypes (and window too) to implement whatever HTML tags/attributes/containers I want.

See my HTML() bookmarklet library (http://www.angelfire.com/ca/redwards/html__.calendar.html) for details.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum