Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing parameters to prototypes

    Is it possible to pass parameters to prototypes? Such as...

    Code:
    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?

    Code:
    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.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    yeah, there's absolutely no difference between a prototyped property/method and any other property/method.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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())

  • #4
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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>';

  • #8
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I take this concept to an extreme...

    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 for details.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •