007Julien Posted August 5, 2013 Share Posted August 5, 2013 (edited) 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 August 5, 2013 by 007Julien Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2013 Share Posted August 5, 2013 That's a weird example in general. Call a method which doesn't return a value, but instead sets a property? I haven't ever done that. Link to comment Share on other sites More sharing options...
007Julien Posted August 5, 2013 Author Share Posted August 5, 2013 Fortunately that is not the Saints schedule! Link to comment Share on other sites More sharing options...
davej Posted August 10, 2013 Share Posted August 10, 2013 (edited) <!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 August 10, 2013 by davej Link to comment Share on other sites More sharing options...
007Julien Posted August 11, 2013 Author Share Posted August 11, 2013 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 More sharing options...
davej Posted August 12, 2013 Share Posted August 12, 2013 (edited) 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 August 12, 2013 by davej Link to comment Share on other sites More sharing options...
007Julien Posted August 12, 2013 Author Share Posted August 12, 2013 The first example is on w3schools.com site ! Link to comment Share on other sites More sharing options...
justsomeguy Posted August 12, 2013 Share Posted August 12, 2013 It's just an illustrative example to show both a method and property being added to the prototype. It's not a useful example, it's just supposed to illustrate the concepts. Link to comment Share on other sites More sharing options...
davej Posted August 12, 2013 Share Posted August 12, 2013 Shouldn't they include a prototype for myProp ? Date.prototype.myProp = null; Link to comment Share on other sites More sharing options...
justsomeguy Posted August 12, 2013 Share Posted August 12, 2013 Ideally they would want to initialize it, yes. Link to comment Share on other sites More sharing options...
007Julien Posted August 13, 2013 Author Share Posted August 13, 2013 It's a shame to choose, as you mentioned, a weird example for illustration ! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now