????????????????????β??????????ɡ??????????????????????????????????????????????????????ɡ?

??????仰????棺???????????????????????????????(TDD)???????????ó????????????κ???????????????????????????????????????????????????????

????????????????jQuery?????????????????????

??????????????????????????????????????

???????????????????д???????

?????????????????????

??????????ζ?????£???????????????????????????????????????Щ??????????????????????????????

?????????????

???????????

?????????/??????

??????????У????????????????????????仰???????????????????????????£???????????????????????£???????????????????????????????Ч?????????????????????????????????????????С?

??????η??????

????????????????????????????????????????????????????????????????Щalert????(this task was most commonly carried out bysprinkling code with alert calls)???????????????????????????????????????????????????????????????(or to observewhere the expected flow of a script diverged from the expected flow)??

?????????????????????????????????????????????????Firebug Lite????????????????????????????????????????console.log???????????????????????????????????????????????

????????????????

???????????????session??????????????????jQuery???????????????????????datetime????(??HTML5??????)????????????data-datetime????????????????????????????????????????????????(??3С???)?滻????innerHTML??

????[javascript] view plaincopy1. jQuery.fn.differenceInWords = (function () {

????2. var units = {

????3. second: 1000??

????4. minute: 1000 * 60??

????5. hour: 1000 * 60 * 60??

????6. day: 1000 * 60 * 60 * 24??

????7. week: 1000 * 60 * 60 * 24 * 7??

????8. month: 1000 * 60 * 60 * 24 * 30

????9. };

????10.

????11. function format(num?? type) {

????12. return num + " " + type + (num > 1 ? "s" : "");

????13. }

????14.

????15. return function () {

????16. this.each(function () {

????17. var datetime = this.getAttribute("datetime") ||

????18. this.getAttribute("data-datetime");

????19. var diff = new Date(datetime) - new Date();

????20.

????21. if (diff > units.month) {

????22. this.innerHTML = "more than a month ago";

????23. } else if (diff > units.week) {

????24. this.innerHTML = format(Math.floor(diff / units.week)?? "week") + " ago";

????25. } else {

????26. var pieces = []?? num?? consider = ["day"?? "hour"?? "minute"?? "second"]?? measure;

????27.

????28. for (var i = 0?? l = consider.length; i < l; ++i) {

????29. measure = units[consider[i]];

????30.

????31. if (diff > measure) {

????32. num = Math.floor(diff / measure);

????33. pieces.push(format(num?? consider[i]));

????34. }

????35. }

????36.

????37. this.innerHTML = (pieces.length == 1 ? pieces[0] :

????38. pieces.slice(0?? pieces.length - 1).join("?? ") + " and " +

????39. pieces[pieces.length - 1]) + " ago";

????40. }

????41. });

????42. };

????43. }());

????

?????????????????????????????????????????±????“?????????(more than a month)”??????????????????????????????????????????????????С????????????????С????????????????????????

??????????????????????????????????????Щ????????"Humanizing"???8????????????“"and undefined”?????console.log????????????????????????????м?????ж????????????????????????????????????????????????????????????

????[javascript] view plaincopy1. var diff = new Date(datetime.replace(/+.*/?? "")) - new Date();

????????????????????????????????????????????“"1 week ago”???????????????????????в??????????????????????????????(So we toss theplugin into production and keep happily hacking on some other part of theapplication.)??

??????????????o???????????“????80С???4854?????291277??”("3 days?? 80 hours?? 4854minutes and 291277 seconds" )??????????????????????????????????С????????????????console.log???????????????????????(????????????Щ????????????????)??????μ??????????ζ????????