B
Branco
Hi, all.
I see two different "idioms" in the code presented in the FAQ (http://
jibbering.com/faq/index.html), and, to me, one of then looks very
strange and somewhat difficult to follow.
The first, more "traditional", idiom goes like this:
<quote>
/** Get IS0 8601 format YYYY-MM-DD from a Date Object */
function formatDate(date) {
var year = date.getFullYear(), sign = "", yyyy, mm, dd;
if(year < 0) {
sign = "-";
year = -year;
}
yyyy = sign + padLeft(year, 4, "0"),
mm = padLeft(date.getMonth() + 1, 2, "0"),
dd = padLeft(date.getDate(), 2, "0");
return yyyy + "-" + mm + "-" + dd;
}
</quote>
As for the other idiom, my limited knowledge can't see the reason
behind it's use (I assume there *are* valid reasons to adopt it).
Specifically:
<quote>
var numberToFixed;
(function() {
numberToFixed = toFixedString;
function toFixedString(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}
function toUnsignedString(n, digits) {
var t, s = Math.round(n * Math.pow(10, digits)) + "",
start, end;
if (/\D/.test(s)) {
return "" + n;
}
s = padLeft(s, 1 + digits, "0");
start = s.substring(0, t = (s.length - digits));
end = s.substring(t);
if(end) {
end = "." + end;
}
return start + end; // avoid "0."
}
// Test results
var d = document;
d.writeln(" numberToFixed(9e-3, 12) => " + numberToFixed(9e-3,
12));
d.writeln(" numberToFixed(1.255, 2) => " + numberToFixed(1.255,
2));
d.writeln(" numberToFixed(1.355, 2) => " + numberToFixed(1.355,
2));
d.writeln(" numberToFixed(0.1255, 3) => " + numberToFixed(0.1255,
3));
d.writeln(" numberToFixed(0.07, 2) => " + numberToFixed(0.07,
2));
d.writeln(" numberToFixed(0.0000000006, 1) => " + numberToFixed
(0.0000000006, 1));
d.writeln(" numberToFixed(0.0000000006, 0) => " + numberToFixed
(0.0000000006, 0));
})();
</quote>
I wonder why not to write the function in the traditional way:
<example>
function numberToFixed(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}
...
</example>
Could someone elighten me on why numberToFixed is declared the way it
is in the FAQ?
Sorry if this was already debated here, but I wasn't able to find any
thread about it -- I did find a thread arguing why functions in the
FAQ used the Function literal declartion, but it seems that issue was
already resolved =)))
Best regards,
Branco.
I see two different "idioms" in the code presented in the FAQ (http://
jibbering.com/faq/index.html), and, to me, one of then looks very
strange and somewhat difficult to follow.
The first, more "traditional", idiom goes like this:
<quote>
/** Get IS0 8601 format YYYY-MM-DD from a Date Object */
function formatDate(date) {
var year = date.getFullYear(), sign = "", yyyy, mm, dd;
if(year < 0) {
sign = "-";
year = -year;
}
yyyy = sign + padLeft(year, 4, "0"),
mm = padLeft(date.getMonth() + 1, 2, "0"),
dd = padLeft(date.getDate(), 2, "0");
return yyyy + "-" + mm + "-" + dd;
}
</quote>
As for the other idiom, my limited knowledge can't see the reason
behind it's use (I assume there *are* valid reasons to adopt it).
Specifically:
<quote>
var numberToFixed;
(function() {
numberToFixed = toFixedString;
function toFixedString(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}
function toUnsignedString(n, digits) {
var t, s = Math.round(n * Math.pow(10, digits)) + "",
start, end;
if (/\D/.test(s)) {
return "" + n;
}
s = padLeft(s, 1 + digits, "0");
start = s.substring(0, t = (s.length - digits));
end = s.substring(t);
if(end) {
end = "." + end;
}
return start + end; // avoid "0."
}
// Test results
var d = document;
d.writeln(" numberToFixed(9e-3, 12) => " + numberToFixed(9e-3,
12));
d.writeln(" numberToFixed(1.255, 2) => " + numberToFixed(1.255,
2));
d.writeln(" numberToFixed(1.355, 2) => " + numberToFixed(1.355,
2));
d.writeln(" numberToFixed(0.1255, 3) => " + numberToFixed(0.1255,
3));
d.writeln(" numberToFixed(0.07, 2) => " + numberToFixed(0.07,
2));
d.writeln(" numberToFixed(0.0000000006, 1) => " + numberToFixed
(0.0000000006, 1));
d.writeln(" numberToFixed(0.0000000006, 0) => " + numberToFixed
(0.0000000006, 0));
})();
</quote>
I wonder why not to write the function in the traditional way:
<example>
function numberToFixed(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}
...
</example>
Could someone elighten me on why numberToFixed is declared the way it
is in the FAQ?
Sorry if this was already debated here, but I wasn't able to find any
thread about it -- I did find a thread arguing why functions in the
FAQ used the Function literal declartion, but it seems that issue was
already resolved =)))
Best regards,
Branco.