You're right, typeof new Function() === "function". *trim*
I didn't know you still lurked around here, Trinithis :)
Printable View
You're right, typeof new Function() === "function". *trim*
I didn't know you still lurked around here, Trinithis :)
I sometimes do :D
Normal people punch things when they get angry!
What on earth does it actually do?
Interesting code. What ticked you off?
Dal: I explained it in an earlier post (the interesting bit, anyway: it also prints out various animal noises, but... no-one cares about that). Trinithis also linked to a book (an excellent book, by the way, which I highly recommend) which covers this amongst other topics.Eh, various things.Quote:
Interesting code. What ticked you off?
Was there a woman involved? I guess I deserved that short answer in trade for mine. Well you certainly haven't given us any reason here not to tick you off. ;)
I especially liked (and I may be seeing the code wrong, so I guess I should say, "what looks to me like") the use of an anonymous function as an object in the global scope. But I will be scratching my head for awhile over exactly why. I thought that there wasn't much point in having an anonymous function other than getting all but its execution out of the global scope.
You are indeed seeing that incorrectly, I believe. The function is called. However, there are some situations in which the pattern you describe is useful; I use it in my 'DOM' toolkit, something like this:Which allows code like:Code:var Dom = (function() {
var r = function(s) {
// CSS selector stuff
};
r.byName = function(s) {
return document.getElementsByName(s);
};
return r;
})();
... where the most useful function is more easily accessible (at the cost of being named less descriptively).Code:var someEl = Dom("#some-id"),
someOtherEls = Dom.byName("and-a-name"),
andSomeMoreEls = Dom(".some-class");
Fantastic stuff, Im sure I may even use this one day but I have no need for it at the moment. Im a noob at javascript, only 4 months experience. Thanks to C/C++ Im doing fine but functions like even the one above is a little too OOP for getting things done.
:)
Well, you'll never have a need for it. It's a convenience. Rather than:You would:Code:function foo(arg) {
if (typeof arg === "string") {
bar();
} else if (typeof arg === "number") {
baz();
} else {
quux();
}
}
It's particularly interesting because it's a convenience that can be used to implement a form of object orientation that's much more powerful than Java's or C++'s.Code:var foo = Generic.create(quux);
foo.specialise(['string'], bar);
foo.specialise(['number'], baz);
I suppose theres only one question remaining in that case;
What can you come up with when your happy? ;)
Thanks for the reply Twey :)