D
Daniel
Lasse Reichstein Nielsen said:That is a step on the way to understanding, making the concepts fit
inside ones head. However, words that make sense in your mind, often
ends up making little or no sense when taken out of that context.
Tell me about it! Hehe =)
Or, as somebody once said: You haven't really understood something
before you can explain it clearly. (He was right!)
Come to think of it, that's pretty much how I know I've really understood
something, which is probably why I keep babbling and babbling on about this,
because I can't say that I'd be able to teach someone else this and know I
was telling the truth Also, I'm pretty sure I could use these skills now
without causing errors, but I guess the quest right now really is that I
want to be able to explain it clearly. So right on!
I think you are putting the parentheses in the wrong places. The code was
var anObject = function(){...} ();
I think you are reading it as
(var anObject = function(){...}) ();
i.e., assign the function value to the anObject variable first, then
call it. This is syntacitcally illegal, since the "var" keyword makes
it a declaration, and declarations have no value. The correct way of
reading it, however, is
var anObject = (function(){...} ());
GREAT way to put it! Actually, I think of the parentheses like this:
var anObject = (function(){...})();
with (function(){...}) being evaluated first, then the return value of that
being evaluated by (); then the return value of that being assigned to
anObject.
But that's not entirely true, because at the point of assigning anObject a
value, which, as I understand, is what is happening here, I don't see any
evaluation being made just yet, at that point I see it as you put it, that
is:
var anObject = (function(){...}()
in the sense that at this particular instant in time, anObject holds a
reference to all definitions inside these parentheses, and these definitions
will stay "untouched" or "unexecuted" or "unevaluated" or "unprocessed"
until anObject is called in another context. So when I say above that,
"...the return value of that being assigned to anObject", that would require
its invocation context to be simply:
anObject();
However, the way we use it, as an object construct -
var myInstance = new anObject();
- I see "...the return value of that being assigned to anObject", but
"anObject" in this context being nothing more than the courier of the
reference it holds inside, i.e. returns to an expression, i.e. assigns to
the given variable. In this context, to properly explain this with
parenthesis, I'd probably write a combination of the former two:
((function(){...})()
- and lose the "var anObject" assignment part because that would be implied,
not as a variable holding the evaluated contents of the parentheses above,
but the yet unevaluated definition contents - the actual evaluation and
solution of the parentheses take place at that particular time and not
before.
That's pretty much how I understand that.
There is no "anObject" function. If you bound the function first, say using
(anObject = function(){...}) ();
(without the "var" so it is legal), then anObject would be a function, but
the "()" would call that function, not the value returned by it. The returned
value is actually lost.
I'm glad you write this, because that's actually exactly what I mean =) (See
why I say you're better at English than I am?
yes?
The execution goes like this:
1 : create anonymous function (a "function expression"):
function() { document.write ... return constructor}
2 : call the function from 1
2.1 : execute document.write("1")
2.2 : create an anonymous function (a "function expression"):
function() {document.write(" 2");}
With you so far.
2.3 : assign the result of 2.2 to the new global variable "constructor".
Global? I thought it'd be a private member of oneOff?
2.4 : return the value of the global variable "constructor". This is the
result of 2.
3 : assign the result of 2 to the local variable "oneOff".
4 : use the value of the variable "oneOff" as a constructor with
no arguments (new ...()). That is, create a new object and
call the function with "this" pointing to the new object.
4.1 : execute document.write(" 2")
This is executed as a result of the instantiation of the constructor object,
right? Not Mr. One-Off?
4.2 : since nothing is returned, the result of the "new"-expression (and
the result of 4) is the new object.
5 : assign the result of 4 to the variable "test".
I'm with you all the way (except for the global constructor variable), and
thank you for putting it like this, because the sequence was a bit messy in
my head, seeing it like this makes it perfectly clear
I would just call it "constructor function"
<snip>I would just called it "object", "instance of myconstructor" or
"constructed object". Shorter is (sometimes) better
Oh yes =)
That is much closer to Javascript than most languages then. Javascript
is a prototype based (as oppesed to class based) object oriented language.
That's what I thought, since they're both ECMA-based, but when I see "class"
being used, I suddenly start wondering... I do that a lot...
In that case, I actually think you *do* get it (apart from a minor
detail about how function application and assignment associates),
Daniel, but for your own sake, work on your ability to precisely
explain what you mean!
Woohoooo!!! =)
You'll go far with that attitude
You just wait and see, one day you guys will be asking ME for help! *L*
Thanks mate!
Daniel
PS: I think this thread is getting really good reading for newbies trying to
understand stuff like this, don't you guys? Your expertise and my daftness
is a winning cocktail! We oughta write some books!