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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    8
    Thanked 1 Time in 1 Post

    Understnading Javascript Prototype & Objects

    Hi, I am teaching myself Javascript and I'm currently learning about Prototypes. I was wondering if anyone would be able to send me some scripts with some practical uses for prototype so that I can study it and try to understand it. Appreciate any help. Thanks, PioStudios

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    A very simple starter:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    String.prototype.properCase = function() 
    {
        // split the string into words after first converting it to all lower case
        // for simple example, we split on all white space characters
        var words = this.toLowerCase().split(/\s/g);
    
        // then loop through all the words...
        for ( var w = 0; w < words.length; ++w )
        {
            // get one word
            var word = words[w];
            // then uppercase first letter and leave other characters alone
            words[w] = word.charAt(0).toUpperCase() + word.substring(1);
        }
        // and return the joined-by-space result
        return words.join(" ");
    }
    
    document.write( "THIS is an eXAMple".properCase() );
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    PioStudios (08-11-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <script type = "text/javascript">
    
    String.prototype.properCase = function() {
    return this.toLowerCase().replace(/^(.)|\s(.)/g, 
    function($1) { return $1.toUpperCase(); });
    }
    
    document.write( "THIS is an eXAMple".properCase() );
    
    </script>
    Or simpler (and better as the above and Old Pedant's scripts cannot cope with hyphens and apostrophes):-


    Code:
    var str1 = "jEAn-paul o'flaNAGan-macDONald"
    str1 = str1.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    alert(str1);
    OP - have a look at
    http://javascriptweblog.wordpress.co...pt-prototypes/
    http://www.javascriptkit.com/javatutors/proto4.shtml

    I have to say that although I understand prototypes I have rarely come across any practical situation which requires using one.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    PioStudios (08-12-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Yes, I know about the sneaky way to use a regular expression in replace, but I (perhaps wrongly) assumed that would be beyond the immediate reach of somebody just learning. Reading the original post again, I can see that he said he is just learning about Prototype, not that he is just learning JavaScript.

    Well, I did say it was a simple starter.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    PioStudios (08-23-2012)


  •  

    Posting Permissions

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