Jump to content

A curious exemple for javascript Date prototype


007Julien

Recommended Posts

This exemple is :

Date.prototype.myMet=function(){if (this.getMonth()==0){this.myProp="January"};if (this.getMonth()==1){this.myProp="February"};if (this.getMonth()==2){this.myProp="March"};if (this.getMonth()==3){this.myProp="April"};if (this.getMonth()==4){this.myProp="May"};if (this.getMonth()==5){this.myProp="June"};if (this.getMonth()==6){this.myProp="July"};if (this.getMonth()==7){this.myProp="August"};if (this.getMonth()==8){this.myProp="Spetember"};// With a typo for September !if (this.getMonth()==9){this.myProp="October"};if (this.getMonth()==10){this.myProp="November"};if (this.getMonth()==11){this.myProp="December"};} // Make a Date object, then call the myMet method:    var d = new Date();    d.myMet();    var monthname = d.myProp;// The result of monthname will be: August // It there not a better way to use an array with something like this ?Date.prototype.getLitteralMonth=function(){    return "January,February,March,April,May,June,July,August,September,October,November,December".split(',')[this.getMonth()];}alert(d.getLitteralMonth())

That is not scripting but writing guessing !

 

At least the proposed method has the merit to justify the numbering of months from 0 to 11 without imposing 12 unnecessary tests !

 

Since your site is very useful. Thanks...

Edited by 007Julien
Link to comment
Share on other sites

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Prototype for Date()</title><script>window.onload = init;function init(){document.getElementById('btn').onclick = go;document.getElementById('btn').onmouseover = clr;}function clr(){document.getElementById('out').innerHTML = '';}function go(){var d = new Date();var f = document.getElementById('sel').value;document.getElementById('out').innerHTML = eval(f);}Date.prototype.getMonthName1 = function(){switch (this.getMonth()){case 0:  return 'January';case 1:  return 'February';case 2:  return 'March';case 3:  return 'April';case 4:  return 'May';case 5:  return 'June';case 6:  return 'July';case 7:  return 'August';case 8:  return 'September';case 9:  return 'October';case 10: return 'November';case 11: return 'December';}}Date.prototype.getMonthName2 = function(){  return ['January','February','March','April','May','June','July','August','September','October','November','December'][this.getMonth()];}Date.prototype.getMonthName3 = function(){  var a = ['January','February','March','April','May','June','July','August','September','October','November','December'];  return a[this.getMonth()];}Date.prototype.getMonthName4 = function(){  return "January,February,March,April,May,June,July,August,September,October,November,December".split(',')[this.getMonth()];}</script></head><body><select id="sel"> <option value="d.getMonthName1()">1</option> <option value="d.getMonthName2()">2</option> <option value="d.getMonthName3()">3</option> <option value="d.getMonthName4()">4</option></select><input type="button" id="btn" value="GetMonthName"/><div id="out"></div></body></html>
Edited by davej
Link to comment
Share on other sites

I am not sure to understand the interest of this four variants ?

Calculation times are probably the same and probably much lower than reaction times of users.

Only the form, seems to favour the choice of solutions 2 or 4 ?

 

The only question is : Can we expect a change in the page of the site never to see this awful code ?

Link to comment
Share on other sites

Are you saying your original example was actual code that was or is on a website somewhere?

 

The posted code with the four versions is provided only for amusement.

Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...