Message from discussion
Using setInterval inside an object
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!news.tele.dk!not-for-mail
Reply-To: "Daniel" <sorry-no-em...@i-get-virus-and-spam.com>
From: "Daniel" <sorry-no-em...@i-get-virus-and-spam.com>
Newsgroups: comp.lang.javascript
References: <3f004263$0$76159$edfadb0f@dread11.news.tele.dk> <k7b3wt48.fsf@hotpop.com> <3f005408$0$76130$edfadb0f@dread11.news.tele.dk> <y8zjv8r9.fsf@hotpop.com> <290e65a0.0306302017.b46bce8@posting.google.com> <3f0145b5$0$97265$edfadb0f@dread12.news.tele.dk> <bdro04$6m8$1$8300dec7@news.demon.co.uk> <3f017100$0$97169$edfadb0f@dread12.news.tele.dk> <bds30l$kqs$1$830fa79d@news.demon.co.uk> <3f019b7d$0$97230$edfadb0f@dread12.news.tele.dk> <bdt8cp$hvi$1@titan.btinternet.com> <3f02b635$0$97201$edfadb0f@dread12.news.tele.dk> <be0119$ln8$1$8302bc10@news.demon.co.uk> <3f044c3c$0$97223$edfadb0f@dread12.news.tele.dk> <be2ndm$4t1$1$8300dec7@news.demon.co.uk>
Subject: Re: Using setInterval inside an object
Date: Fri, 4 Jul 2003 14:36:47 +0200
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Lines: 161
Message-ID: <3f0574e1$0$97272$edfadb0f@dread12.news.tele.dk>
Organization: TDC Internet
NNTP-Posting-Host: 80.162.71.99
X-Trace: 1057322210 dread12.news.tele.dk 97272 80.162.71.99
X-Complaints-To: abuse@post.tele.dk
"Richard Cornford" <Rich...@litotes.demon.co.uk> wrote in message
news:be2ndm$4t1$1$8300dec7@news.demon.co.uk...
> Ops, that's my fault. If you look closely at the way I have spelled
> function you will notice that the t and the i have been transposed. One
> of the hazards of touch typing into a word processor instead of a
> syntax-highlighting text editor. Though I am surprised that I did not
> notice Word complaining about the spelling, but there is a tendency to
> ignore highlighted spelling mistakes when entering code in Word. Sorry
> if that was confusion, correct the spelling an it will work as
> advertised.
No, actually it's my fault ;) I did see the typo and fixed it (I usually
write "functino" myself), but I called it using "new" which was obviously a
mistake.
> My reason for stressing that the assignment operation had a left hand
> side (var MyObject) and a right hand side (all the rest of the
> statement) was to try and explain how MyObject was only ever assigned
> the end result of the right hand side. There is not "hijacking" because
> the only value that is ever associated with the MyObject variable is a
> reference to a function object that was originally created as the inner
> function "constructor" within the execution context of the one-off
> function execution.
And that's *almost* how I saw it. I didn't see a sequence of assignments
going on, but a sequence of evaluations, with one final assignment. I simply
needed to visualize the "intermediate results" to properly conceive the
sequence and the final effect :P I just couldn't see that the Mr. One-Off
and his trail of closure came into play at the moment of assigning MyObject
its value. I wanted Mr. One-Off to come along for the ride =)
In other words, the final knot I needed to tie (should've been the first
knot) was that after something like this -
var blahblah = function() {
constructor = function() {
};
return constructor;
}();
- blahblah already holds the reference to constructor, and I wanted it to
hold a reference to the function expression until blahblah was called for
the first time. Which is also why the hijacking term was not only confusing
but plain out wrong - there's no reference to hijack =)
I also think that I held on to that illusion so long because I couldn't see
anything functionally proving it to be false, the difference between
illusion and fact was essentially the point of time the anonymous function
was "evaluated" or "executed", and delaying the evaluation demanded that a
ficticious reference to the function expression had to exist. If such a
reference did indeed exist, and it worked the way I wanted it too, I don't
think it would've caused any programmatical problems (at least not in this
context), it would probably just require a rewrite of the Javascript
specifications ;)
The facts are much more logic and easier to understand - it's kinda funny
(or, very irritating ;) that I didn't see them the first time.
> You seem to be seeing too much happening at once and Lasse is right,
> parenthesising the original statement should make the real sequence of
> events clearer.
Which is also what you say here =)
And here:
> At the point of executing:-
>
> var objectInstance = MyObject();
>
> - the MyObject variable already contains a reference to the inner
> function that was defined with the name "constructor". The one-off
> function has already been executed and done its work, At this point The
> one-off function has no accessible references and will never be executed
> again.
...
> The template for an ECMA Script object is much more loosely defined, and
> it is also mutable (though you wouldn't often want to do that). If my
Don't want to start another long thread that should have its own topic, but
what's "mutable"?
> original class was defined in the more traditional JavaScript way, as a
> separate constructor function and as properties of that function and its
> prototype object (the private members must now be public members as
> there are no closures to contain them), the results would look like:-
>
> function MyObject(){
> this.id = id;
> this.self = this; //pointless in an object with no inner functions
> this.index = MyObject.incrementCounter();
> }
> MyObject.prototype.getIndex = function(){
> return this.index;
> }
> MyObject.counter = 0;
> MyObject.incrementCounter = function(){
> MyObject.counter++;
> }
> MyObject.getNoOfInsts = function(){
> return counter;
> }
> MyObject.prototype.getId = function(){
> return this.id;
> }
It's really nice to see it like this, very familiar =)
> >Man, I really want the next words you say to be, "Actually I think
> >you *do* get it, Daniel, but for God's sake, man, work on your
> >ability to precisely explain what you mean!"
>
> Sorry, I am not going to say that until I can see that you have
> understood the behaviour of the in-line one-off function call. You are
> getting close and Lasse has posted a very explicit description so I am
> confident that you will have it under your belt next time.
How about now? =)
> As far as precise explanations, that goes both ways. If I was better at
> explaining then you might find understanding easier. Seeing where I fail
> to get my point across helps me to refine my explanation, which makes
> this whole exchange worthwhile in itself. I think that you are doing
> your half of the conversation very well. In describing your
> understanding at every stage I think that I can see which aspects of the
> process I am failing to properly convey.
Good to hear this is not just to my benifit. =)
> > If nothing else, I truly understand why
> >
> > alert(function(){ return function() { return true; }}()());
> >
> > Gives me an alert box with "true" written in it.
>
> Excellent! One more small step and your there.
Actually, when I said I understood that, I must have been either lying or
already "there" but too daft to realize it, because if I truly understood
that as I understood (read: misunderstood) the real issue, then I would have
had to believe that the alert box would write out "function" (at least if
suffixed by a typeof), since that would have been the "reference to hijack"
according to my illusion! Or maybe I just had to connect the simple with the
advanced :)
At least now, I TRULY truly understand it ;)
Thank you, both of you!
Daniel
--
There are 10 kinds of people: Those who know binary and those who don't.