View Full Version : Passing parameters to prototypes

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

var myString="Hello World!";

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

return this.style.color=theColor;

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

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

02-26-2003, 03: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())

02-26-2003, 05: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?


... or am I missing something...

02-26-2003, 05:59 AM
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.

02-26-2003, 06:55 AM

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?

02-26-2003, 07:02 AM
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>';

02-27-2003, 04: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.