Drycodez Posted June 28, 2011 Share Posted June 28, 2011 Is it possible to declear date, time, day without using variables? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 28, 2011 Share Posted June 28, 2011 Why can't you use a variable? What are you trying to do? Link to comment Share on other sites More sharing options...
Drycodez Posted June 28, 2011 Author Share Posted June 28, 2011 Why can't you use a variable? What are you trying to do?just want to know if its a must to use variable to store the date. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 28, 2011 Share Posted June 28, 2011 If you want to store anything you need to use a variable. If you want to just print it or something else you can use a literal value. Link to comment Share on other sites More sharing options...
Drycodez Posted June 28, 2011 Author Share Posted June 28, 2011 If you want to store anything you need to use a variable. If you want to just print it or something else you can use a literal value. Link to comment Share on other sites More sharing options...
Drycodez Posted June 28, 2011 Author Share Posted June 28, 2011 What do u mean by literal value ? Link to comment Share on other sites More sharing options...
jeffman Posted June 28, 2011 Share Posted June 28, 2011 It is possible to create a Date object without assigning it to a variable. The following alerts the current year. All the parentheses are required: alert( (new Date() ).getFullYear() ); Realistically, though, there is ALMOST NEVER not a reason to create a variable, or even several variables, in a situation like this. Code like ^^ is hard to read and gains you nothing.This next bit makes your intentions perfectly clear, and the additional overhead is so microscopically small that it will never be noticed. var myDate = new Date();var myYear = myDate.getFullYear();alert(myYear); There was a time when processors were small and RAM was expensive. It made sense to be stingy with characters and variables. Prgrammers even had contests to see who could write the shortest code to get the job done. The results were "cool" but generally unreadable. Those days are gone. The hardware and software that run your cellphone are more complex than the desktop PC I bought in 1988. Today's efficiency is all about humans and almost never about software (unless maybe you're programming a Mars Rover). Link to comment Share on other sites More sharing options...
justsomeguy Posted June 28, 2011 Share Posted June 28, 2011 A literal value is a string of characters, a number, true/false, etc. Link to comment Share on other sites More sharing options...
brucemand Posted June 29, 2011 Share Posted June 29, 2011 ... ...alert( (new Date() ).getFullYear() ); Realistically, though, there is ALMOST NEVER not a reason to create a variable, or even several variables, in a situation like this. Code like ^^ is hard to read and gains you nothing.This next bit makes your intentions perfectly clear, and the additional overhead is so microscopically small that it will never be noticed.... ... is it "nothing" or "microscopically small" ?and what negligible amounts are we talking here ?what additional processing time is taken when referencing a variable as opposed to a literal ?(let's say on an average PC of 'today' ) Link to comment Share on other sites More sharing options...
justsomeguy Posted June 29, 2011 Share Posted June 29, 2011 You're talking about nanoseconds or microseconds, and several bytes of memory. Link to comment Share on other sites More sharing options...
jeffman Posted June 29, 2011 Share Posted June 29, 2011 Time so small you'd need to repeat the process 100K - 1M times in order to measure enough milliseconds to report the difference. People actually do this. It's called benchmarking. Link to comment Share on other sites More sharing options...
jeffman Posted June 29, 2011 Share Posted June 29, 2011 Well, I benchmarked it. About 20 trials of 100,000 iterations each. I'm sitting at an old Mac G4 right now. The browser versions cannot be higher because the old chip won't run new system software. Actually, that's good for this sort of test. Worst case scenarios!The "short" version looks like this: str = (new Date() ).getFullYear(); The "long" version: D = new Date();Y = D.getFullYear();str = Y; FIREFOX 3.64There was one trial where the short version was slower than the long version by 2 milliseconds. All the other trials put the short version 20-100 milliseconds faster.SAFARI 4.0The short version usually won, but never by more than about 40 milliseconds. Sometimes it lost, but by a smaller margin. There were a lot of very close ties, 1-3 milliseconds difference.BUT REMEMBERI had to run the test 100,000 times even to see those differences on an 8-year-old computer. The maximum difference in efficiency to be inferred when the job needs to be done exactly ONCE? 100 millionths of a second. (Sorry if I'm off by a decimal place or so.)This is what I mean by microscopically small. Amounts of time that are so small they cannot be perceived by the human brain.Now, if you want to improve efficiency when you're animating a duck across the screen, that's a different story, requiring different techniques altogether.Did we learn something today? Link to comment Share on other sites More sharing options...
brucemand Posted June 29, 2011 Share Posted June 29, 2011 You're talking about nanoseconds or microseconds, and several bytes of memory.ok, that's really insignificant !still, i believe it's important to know (and remember) First Principles.(i think one of you Angels mentioned your pet peeve with Dynamic Drive "off the shelf" scripts ?)Well, I benchmarked it. ......BUT REMEMBERI had to run the test 100,000 times even to see those differences on an 8-year-old computer. The maximum difference in efficiency to be inferred when the job needs to be done exactly ONCE? 100 millionths of a second. (Sorry if I'm off by a decimal place or so.)This is what I mean by microscopically small. Amounts of time that are so small they cannot be perceived by the human brain.Now, if you want to improve efficiency when you're animating a duck across the screen, that's a different story, requiring different techniques altogether.wow, now THAT'S a comprehensive reply !!MUCH appreciated !Did we learn something today? *always*Thanks, "Jim" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.