Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2012, 09:19 PM   PM User | #1
PioStudios
New to the CF scene

 
Join Date: Apr 2012
Posts: 9
Thanks: 8
Thanked 1 Time in 1 Post
PioStudios is an unknown quantity at this point
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
PioStudios is offline   Reply With Quote
Old 08-10-2012, 09:33 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
PioStudios (08-11-2012)
Old 08-12-2012, 10:17 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
PioStudios (08-12-2012)
Old 08-13-2012, 12:17 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
PioStudios (08-23-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 PM.


Advertisement
Log in to turn off these ads.