Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Using setInterval inside an object
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Lasse Reichstein Nielsen  
View profile  
 More options Jul 3 2003, 12:10 pm
Newsgroups: comp.lang.javascript
From: Lasse Reichstein Nielsen <l...@hotpop.com>
Date: 03 Jul 2003 18:14:26 +0200
Local: Thurs, Jul 3 2003 12:14 pm
Subject: Re: Using setInterval inside an object

"Daniel" <sorry-no-em...@i-get-virus-and-spam.com> writes:
> First, let me say that my use of words like "morph" and "become"
> shouldn't be taken too literally, at least not in the programmatical
> context; the context should be my way of putting my understanding
> into human, easily digestible, words.

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. :)

Or, as somebody once said: You haven't really understood something
before you can explain it clearly. (He was right!)

> But still, I don't see the anonymity of the "original" MyObject function
> being the result of function expression, since it isn't anonymous before
> after it's been called. Up until that point it does exist as MyObject, as a
> definition yes, but it still has a reference...

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(){...} ());

i.e., create anonymous function, call it, and assign the return value to
the anObject variable.

I had to read it twice too, to be sure what happened. I would have put
the parentheses there to disambiguate.

> To me, a truly anonymous function expression would be something like

>     myArray.sort(function(a,b){ return subCmp(a[label],b[label]); });

> ...And then you write this:

Indeed.

> > var anObject = funciton(){
> >     return new Array();
> > }();
> >   ^- It is the pair of brackets after the function expression that
> > execute it (in-line and once only).

> But how can that be true? As I see it, the (); calls whatever's returned by
> the "anObject" function,

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.

> And, if I write

>     var oneOff = function() {
>         document.write("1");
>         constructor = function() {
>             document.write(" 2");
>         }
>         return constructor
>     }();

>     var test = new oneOff();

> - I get "1 2", which means oneOff is parsed before the inner function, 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");}
2.3 : assign the result of 2.2 to the new global variable "constructor".
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")
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".

> So, as I see it, if,

>     var oneOff = function(){   /*code*/   }();

this is somehow similar to the pseudo-code:
      var oneOff = /* code */ ;
except that code need not be an expression. It can contain statements and
local variable declarations. Writing
 function () { ... }()
merely creates a local block and a scope.

...

> What I meant by "object constructor *function* object" and "object
> *constructor*" was this:

>     var blahblah = function() {
>         constructor = function() {
>         };
>         return constructor;
>     }
>     var myconstructor = blahblah();
>     ^ That would become my "object constructor *function* object"

I would just call it "constructor function"

>     var myinstance = new myconstructor();
>     ^ And that would become my "object *constructor*"

I would just called it "object", "instance of myconstructor" or
"constructed object". Shorter is (sometimes) better :)

> I confused myself there, actually =) I'm not used to classes, since in
> Actionscript everything is essentially an object (or a prototype). I just
> saw it in your code, and thought, well... Don't know what I thought, really
> :P

That is much closer to Javascript than most languages then. Javascript
is a prototype based (as oppesed to class based) object oriented language.

I sometimes wish there was a clone method on all objects, but one can
make it oneself:

 Object.prototype.clone = function () {
    function myConstructor = function(){};
    myConstructor.prototype = this;
    return new myConstructor();    
 }

> 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!"
> If nothing else, I truly understand why

>     alert(function(){ return function() { return true; }}()());

> Gives me an alert box with "true" written in it.

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!

> Argh! :) It's tough work getting smarter. But FUN!!!
> PS: Richard, thank you for all this, it's really great to have my mind
> challenged - it's been a while ;)!

You'll go far with that attitude :)

/L
--
Lasse Reichstein Nielsen  -  l...@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.