The code idioms in the FAQ

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.
 
G

Garrett Smith

Branco said:
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>
The indentation should have been one character fewer, to avoid wrapping.
Could someone elighten me on why numberToFixed is declared the way it
is in the FAQ?

The function was placed in a closure. This has the effect of limiting
the scope of the internal methods.

http://www.jibbering.com/faq/faq_notes/closures.html

Explains how closures work. As stated previously, there is a mistake in
the section explaining the prototype chain.
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 =)))

What is a Function literal declaration? FunctionExpression?

Garrett
 
D

Dr J R Stockton

Sat said:
The function was placed in a closure. This has the effect of limiting
the scope of the internal methods.

And of totally confusing the sort of people for whom the FAQ should be
intended.

There is no need to have an inaccessible used-once function, either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top