www.webdeveloper.com
Page 5 of 15 FirstFirst ... 34567 ... LastLast
Results 61 to 75 of 221

Thread: Here are 2 prototype-functions I created.

  1. #61
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Here are a few more I use with Dates:

    Code:
     Date.prototype.MonthName =
      function(){
        var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        return arrMonths[this.getMonth()];
      }
    
     Date.prototype.DayName =
      function(){
        var arrDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
        return arrDays[this.getDay()];
      }
    
     Date.prototype.FirstDayInt =
      function(){
        this.setDate(1);
        return this.getDay();
      }
    
     Date.prototype.FirstDayStr =
      function(){
        var arrDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
        this.setDate(1);
        return arrDays[this.getDay()];
      }
    Eric
    Last edited by A1ien51; 05-12-2005 at 06:09 PM.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  2. #62
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534

    Arrow

    ,"July"."August",
    slight error, anyway, if you send an argument of false for this one it will give you short month names:

    Code:
    Date.prototype.MonthName=function(){
    	var arrMonths = arguments[0] ?
    	new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") :
    	new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    	return arrMonths[this.getMonth()];
    }

  3. #63
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    whoops, fat fingered that one when I typed it here.
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  4. #64
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Hey, I added an index for all the prototype-funtions through out this thread to the first post.
    Enjoy!

  5. #65
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Code:
    String.prototype.toWordCaps=function(){
    	var r="", s=this.toLowerCase(), l=s.length, w=true, t;
    	for (var i=0; i<l; i++) {
    		t=s.substr(i,1);
    		r+=(w)? t.toUpperCase() : t;
    		w=(t==" "); // you can add other options here like (t==" " || t=="-");
    	}
    	return r;
    }
    
    var str="hello my name is jiffy";
    document.write(str.toWordCaps());
    This will write to the page: "Hello My Name Is Jiffy", with the first letter of each word capitalised.

  6. #66
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    A shorter method:

    Code:
    <script type="text/javascript"><!--
    String.prototype.toWordCaps=function(){
     return this.replace(/\b([a-z])/gi, function(s){return s.toUpperCase()});
    }
    
    var str="hello my name is jiffy";
    document.write(str.toWordCaps());
    //--></script>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  7. #67
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    That is good (I should learn RegExp some day) although there is a flaw, it doesnt toLowerCaps the rest of the letters, what would be the best way to do that?

  8. #68
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    You Should call the toLowerCase function before using the function then
    It isn't flawed at all.
    It does exactly what it says, it only touches the first letters of each word and leaves the rest unchanged.

  9. #69
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    I guess you are right, how would you do this in Regular Expression: ?

    Code:
    <script type="text/javascript"><!--
    String.prototype.toInverseCase=function(){
    	var r="",	l=this.length, t, T;
    	for (var i=0; i<l; i++){
    		t=this.substr(i,1);
    		T=t.toUpperCase();
    		r+=(t==T)?t.toLowerCase():T;
    	}
    	return r;
    }
    
    var str="Hello My Name Is Jiffy";
    document.write(str.toInverseCase());
    //--></script>

  10. #70
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Like this:

    Code:
    <script type="text/javascript"><!--
    String.prototype.toInverseCase = function (){
     return this.replace(/([a-z]{1})/gi, function (s){return s==s.toLowerCase()?s.toUpperCase():s.toLowerCase()});
    }
    
    var str="Hello My Name Is Jiffy";
    document.write(str.toInverseCase());
    //--></script>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  11. #71
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Thanks, I believe this would be more efficient as it would never call .toLowerCase() twice for the same character:

    Code:
    <script type="text/javascript"><!--
    String.prototype.toInverseCase = function (){
     return this.replace(/([a-z]{1})/gi, function (s){var t=s.toLowerCase(); return s==t?t.toUpperCase():t;});
    }
    
    var str="Hello My Name Is Jiffy";
    document.write(str.toInverseCase());
    //--></script>

  12. #72
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Correct, though I doubt it will make that much of a difference (unless you plan on running the script a good 10,000 times, of course Although that still probably wouldn’t affect the script by more than some 20 milliseconds).
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  13. #73
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Its about 60ms for IE and about 250ms for FF

  14. #74
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    IE’s scripting engine has a tendency to be more snappy than Firefox’s (by the way, the preferred abbreviation for Firefox is “Fx” not “FF”).
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  15. #75
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by tilspam@ofir.dk
    Timing for optimization purposes:

    Code:
    Function.prototype.time =
      function() {
        var sTime = new Date();
        this.apply(null,arguments);
        var eTime = new Date();
        return eTime.getTime() - sTime.getTime();
      };
    Say you have a function f. To time the function with arguments arg0, arg1 & arg2 do

    Code:
    f.time(arg0,arg1,arg2);
    Because calling eTime() actually adds to the time of the function we must deduct the time it takes to call eTime() (as small as it is it will lessen miscalculation).

    So if we take t1 before our function then t2 after the function and t3 after t2 we can use:

    t2 - t1 - (t3 - t2) = 2*t2 - t3 - t1;

    Code:
    Function.prototype.time = function() {
    	var t1 = new Date();
    	this.apply(null,arguments);
    	var t2 = new Date();
    	var t3 = new Date();
    	return 2*t2 - t3 - t1;
    };
    Last edited by BigMoosie; 05-15-2005 at 01:02 AM.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 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