www.webdeveloper.com
Page 14 of 15 FirstFirst ... 412131415 LastLast
Results 196 to 210 of 221

Thread: Here are 2 prototype-functions I created.

  1. #196
    Join Date
    May 2004
    Location
    FL
    Posts
    3,453
    If you guys will add a few comments about the prototype and what it does, I will publish some of them over on JavaScript Source .

    (Also include a name and link for credit for the author, if you wish.)

  2. #197
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    I started with the last entry:

    #88String.prototype.toStudlyCaps by Webnerd
    alert("hi how Are you?".toStudlyCaps())// alerts "Hi How Are You?"

    Edit:
    This is unpractical for the index page. The majority of the functions require a long paragraph of explanation. My index page would exceed the maximum amount of characters and run out of room. If the function name sounds interesting enough, just click away and read the author's post and explanation.
    Last edited by Ultimater; 06-05-2006 at 03:27 PM.

  3. #198
    Join Date
    May 2004
    Posts
    18
    Quote Originally Posted by Webnerd
    Add this to the prototypes list:

    Code:
    String.prototype.toStudlyCaps=function(){
    
    	var val=this;	var results=this.match(/\b(\w+)\b/g); var expr="";
    
    	for(i=0;i<results.length;i++){
    
    		expr=eval("/\\b"+results[i]+"\\b/");
    
    		val=val.replace(expr,results[i].substr(0,1).toUpperCase()+results[i].substr(1,results[i].length).toLowerCase());
    	
    	}
    	return val;
    
    }
    
    alert("hi how Are you?".toStudlyCaps()); // alerts "Hi How Are You?"
    Just in case you are interested I thought you might like to see how this could be simplified. Instead of using an eval (which is rarely a good solution, and in your case it would have been better to create an new RegExp()) you can use a regular expression and call a function in the replacement argument, like so:

    Code:
    function wordify(str){
      return str.replace(/\b\w/g, function(l){ return l.toUpperCase(); } );
    }

  4. #199
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    That function was already done by me then Jona under the title of toWordCaps on this page http://www.webdeveloper.com/forum/sh...112#post368112

  5. #200
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    A Better mergeData()

    Code:
    Array.prototype.mergeData=function(){
    	this.sort();
    	var n=this.length, i=n;
    	while (i>1) if (this[--i]===this[i-1]) this[i]=this[--n];
    	this.length=n;
    	return this;
    }

  6. #201
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Due to the recent change done to the forums Editing posts, I can't edit the index of this thread any more.
    Can I get a dedicated moderator to edit my 2nd post of this thread and add the following to my list:
    Code:
    [*]#89[post=586096]Array.prototype.mergeData[/post] by BigMoosie (revised)
    Edit: Added.
    Last edited by David Harrison; 06-25-2006 at 10:47 PM.

  7. #202
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    An Object doesn't have the normal Array prototype functions like join and push, pop, splice etc which makes it hard to work with.
    Object.prototype.getKeys returns a list of all its keys in an array.
    Object.prototype.getValues returns a list of all its values in an array.
    Code:
    <script type="text/javascript">
    Object.prototype.getKeys=function(){
    var myKeys=new Array();
    for(var i in this){
    if(i in Object)continue;
    myKeys[myKeys.length]=i;
    }
    return myKeys;
    }
    
    Object.prototype.getValues=function(){
    var myValues=new Array();
    for(var i in this){
    if(i in Object)continue;
    myValues[myValues.length]=this[i];
    }
    return myValues;
    }
    </script>
    <script type="text/javascript">
    var myObject={"key1":"value1", "key2":"value2"};
    alert(myObject.getKeys());
    alert(myObject.getValues());
    </script>
    Last edited by Ultimater; 06-27-2006 at 02:48 PM.

  8. #203
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Many people would discouarage modifying the Object prototype (why?).

    Of course people can just implement the workaround you did, if(i in Object)continue; but almost all scripts do not, so adding these may break them.

  9. #204
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Based on PHP's base64_encode and base64_decode functions, I present to you:
    String.prototype.base64_encode and String.prototype.base64_decode in JavaScript.
    Code:
    <script type="text/javascript">
    String.prototype.base64_encode=function(){
    var charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    if(this.length=="")return "";
    for(var i=0,t,l=this.length,binstr="";i<l;i++){
    t=this.charCodeAt(i);
    if(t>255)return null;
    binstr+=("00000000"+(t.toString(2))).substr(-8,8);
    }
    l=binstr.length;var base64str="";
    for(i=0;i<l;i+=6)t=binstr.substr(i,6),base64str+=charset[parseInt((t+"000000").substr(0,6),2)];
    var requiredPadding=(6-t.length)/2;
    if(requiredPadding==1)base64str+="=";
    else if(requiredPadding==2)base64str+="==";
    return base64str;
    }
    
    
    String.prototype.base64_decode=function(){
    var charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    if(this.length==0)return "";
    if(this.match(/[^a-zA-Z0-9\+\/\=]/))return null;
    var base64str=this.replace(/[^a-zA-Z0-9\+\/]/,"");
    for(var i=0,l=base64str.length,binstr="";i<l;i++)binstr+=("000000"+charset.indexOf(base64str.charAt(i)).toString(2)).substr(-6,6);
    l=binstr.length;var str="";
    for(i=0;i<l;i+=8)str+=String.fromCharCode(parseInt((binstr.substr(i,8)+"00000000").substr(0,8),2));
    return str
    }
    </script>
    <script type="text/javascript">
    var base64str="Hello World!".base64_encode();
    alert(base64str);//SGVsbG8gV29ybGQh
    alert("SGVsbG8gV29ybGQh".base64_decode());//Hello World!
    </script>

  10. #205
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    String.prototype.toXMLDocument For turning a String into an XMLDocument (full example included)
    Note that it will even serve IE users with ActiveX disabled because it also checks for XML Data Island support. Read up on IE's XML tag.
    Code:
    <script type="text/javascript">
    String.prototype.toXMLDocument=function(){
    var d;
    /* Firefox */
    try{
    d = (new DOMParser()).parseFromString(this, "text/xml");
    return d;
    }catch(e){};
    
    /* XML Data Islands support (IE5+) */;
    try{
    d = document.createElement("xml");
    d.appendChild(document.createTextNode(this));
    document.documentElement.appendChild(d)
    document.documentElement.removeChild(d)
    d=d.XMLDocument;
    if(d.documentElement)return d;
    }catch(e){};
    
    /* ActiveX enabled browsers */;
    try{
    d = new ActiveXObject("msxml.domdocument")
    d.loadXML(this); 
    if(d.documentElement)return d;
    }catch(e){};
    
    /* Safari */;
    try{
    var uriURL = "data:text/xml;charset=utf-8," + encodeURIComponent(this);
    d = new XMLHttpRequest();
    d.open("GET", uriURL, false);
    d.send(null);
    d=d.responseXML;
    if(d.documentElement)return d;
    }catch(e){};
    
    return new Object();//null object
    }//func
    </script>
    <!-- defining code above, example usage below -->
    <script type="text/javascript">
    var str='<?xml version="1.0" encoding="utf-8"?>';
    str+='\n<xmlcontainer>';
    str+='\n<tagy>This is the value of the tag TAGY<\/tagy>';
    str+='\n<\/xmlcontainer>';
    var xmldoc=str.toXMLDocument();
    alert(xmldoc.documentElement.getElementsByTagName("tagy")[0].firstChild.data)
    </script>
    XML Data Islands can also load external XMLDocuments.
    Code:
    var x=document.createElement("xml");
    x.src="myfile.xml";
    x.onreadystatechange=function(){
    alert(x.readyState+":"+x.XMLDocument.readyState);
    if(x.XMLDocument.readyState==4)
    alert(x.XMLDocument.documentElement.tagName);
    }
    document.documentElement.appendChild(x);
    document.documentElement.removeChild(x);
    I'll hopefully have a cross-browser prototype up soon.
    Last edited by Kor; 07-04-2007 at 11:01 AM. Reason: User''s demand

  11. #206
    Join Date
    May 2006
    Posts
    7

    prototypes

    Ultimater!
    I agree wit u. These prototypes have been of great help to some of the developers who actually are not too much into javascript.
    great work n cheers!

  12. #207
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    String.prototype.repeat is based on PHP's function: str_repeat. The power of the following technique is its high performance and minute coding when compared to unprofessional loops.
    Code:
    <script type="text/javascript">
    String.prototype.repeat=function(a){
    return new Array(a+1).join(this)
    };
    alert("-=".repeat(10))//-=-=-=-=-=-=-=-=-=-=
    </script>
    Of coarse you could also do:
    Code:
    <script type="text/javascript">
    alert(new Array(11).join("-="))
    </script>
    However that gives the mistaken impression of eleven joints. An array consisting of ten elements will only join nine times just-like there are only four gaps between one's five fingers. All the more reason for String.prototype.repeat.
    Last edited by Ultimater; 10-10-2007 at 05:55 AM.

  13. #208
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Element.prototype works in browsers other than IE:

    This function checks that a dom node is a child of another:

    Code:
    Element.prototype.hasChild = function(e) {
        while ( (e=e.parentNode) && this!=e);
        return !!e;
    };

  14. #209
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,437

    Lightbulb splitAtChar() function

    Slight modification to the 'splitAt()' function

    Code:
    String.prototype.splitAtChar=function() {
    	return this.split(this.charAt(this.indexOf(arguments[0])));
    }
    Usage:
    Code:
        var str = 'abc|defdef|ghighighi';
        var tmp = str.splitAtChar('|');
        alert(str+'\n\nsplitAt("|"): '+tmp+'\n\n'+tmp.join('\n'));
    Allows splitting the string at fields of different lengths.

  15. #210
    Join Date
    Nov 2011
    Posts
    39
    That looks really good. I wish I could program as good as that. I turned in my assignment with the code I posted at the end of the last thread.

    I am trying to learn enough to get me through this term with a decent grade so I can get my degree. This 39 year old is one old dog and it is hard for an old dog to learn new tricks.

    Anyhow, greetings from New Mexico!

    I hope things are well in your world.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles