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
Question on closure
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
justaguy  
View profile  
 More options Sep 15 2012, 4:01 pm
Newsgroups: comp.lang.javascript
From: justaguy <lichunshe...@gmail.com>
Date: Sat, 15 Sep 2012 13:01:35 -0700 (PDT)
Local: Sat, Sep 15 2012 4:01 pm
Subject: Question on closure
I intend to get back on javascripting... what could be a good resource
to get my hands on this?
Yes, googling provides some interesting links...

Here's one specific question, closure, credit of Morris Johns.

function sayHello(name) {
  var text = 'Hello ' + name;
  var sayAlert = function() { alert(text); }
  sayAlert();

}

With closure, the sayAlert local var can be used Multiple times if
needed vs. alert(text).
Could we see a more meaningful example?

Thanks in advance.


 
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.
JJ  
View profile  
 More options Sep 17 2012, 8:57 am
Newsgroups: comp.lang.javascript
From: JJ <jaejunks_at@_googlemail_dot._com>
Date: Mon, 17 Sep 2012 12:57:37 +0000 (UTC)
Local: Mon, Sep 17 2012 8:57 am
Subject: Re: Question on closure

I think closure is more like private vs public access where mostly used
to prevent direct access to a variable like below example.

Here, the MyVal object stores a number where it can only be increased or
decreased by one and get queried. The number is prevented from being
directly modified. i.e.: set to specific count.

(function() {
  var theValue = 0;
  myVal = {
    getVal: function() {
      return theValue;
    },
    increase: function() {
      return ++theValue;
    },
    decrease: function(val) {
      return --theValue;
    }
  };


 
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.
justaguy  
View profile  
 More options Sep 17 2012, 12:14 pm
Newsgroups: comp.lang.javascript
From: justaguy <lichunshe...@gmail.com>
Date: Mon, 17 Sep 2012 09:14:00 -0700 (PDT)
Subject: Re: Question on closure
On Sep 17, 8:57 am, JJ <jaejunks_at@_googlemail_dot._com> wrote:

Very interesting.  In this case, function is being used as an
expression (instead of a statement), is that understanding correct?
Also, may we call "getVal", "increase" methods?

Thanks.


 
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.
Gene Wirchenko  
View profile  
 More options Sep 17 2012, 1:02 pm
Newsgroups: comp.lang.javascript
From: Gene Wirchenko <ge...@ocis.net>
Date: Mon, 17 Sep 2012 10:02:13 -0700
Local: Mon, Sep 17 2012 1:02 pm
Subject: Re: Question on closure
On Mon, 17 Sep 2012 12:57:37 +0000 (UTC), JJ

<jaejunks_at@_googlemail_dot._com> wrote:

[snip]

     Could you please post the rest?  How do you access the value?

Sincerely,

Gene Wirchenko


 
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.
Christoph Becker  
View profile  
 More options Sep 17 2012, 1:22 pm
Newsgroups: comp.lang.javascript
From: Christoph Becker <cmbecke...@gmx.de>
Date: Mon, 17 Sep 2012 19:23:01 +0200
Local: Mon, Sep 17 2012 1:23 pm
Subject: Re: Question on closure

Gene Wirchenko wrote:
> Could you please post the rest?  How do you access the value?

Try the following variation of JJ's code:

function Counter() {
 var theValue = 0;
 return {
   getVal: function() {
     return theValue;
   },
   increase: function() {
     return ++theValue;
   },
   decrease: function() {
     return --theValue;
   }
 };

}

It can be used in the following way:

var c1 = Counter(), c2 = Counter();
c1.getVal()   ===> 0
c1.increase() ===> 1
c1.getVal()   ===> 1
c2.getVal()   ===> 0

--
Christoph M. Becker


 
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.
Christoph Becker  
View profile  
 More options Sep 17 2012, 1:31 pm
Newsgroups: comp.lang.javascript
From: Christoph Becker <cmbecke...@gmx.de>
Date: Mon, 17 Sep 2012 19:32:02 +0200
Local: Mon, Sep 17 2012 1:32 pm
Subject: Re: Question on closure

justaguy wrote:
> I intend to get back on javascripting... what could be a good resource
> to get my hands on this?
> Yes, googling provides some interesting links...

The c.l.js FAQ also does:
<http://jibbering.com/faq/#ecmascriptResources> ;-)

--
Christoph M. Becker


 
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.
Gene Wirchenko  
View profile  
 More options Sep 17 2012, 2:13 pm
Newsgroups: comp.lang.javascript
From: Gene Wirchenko <ge...@ocis.net>
Date: Mon, 17 Sep 2012 11:13:17 -0700
Local: Mon, Sep 17 2012 2:13 pm
Subject: Re: Question on closure
On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker

<cmbecke...@gmx.de> wrote:
>Gene Wirchenko wrote:
>> Could you please post the rest?  How do you access the value?

>Try the following variation of JJ's code:

[snip]

     Thank you.  That worked nicely.  I still do not understand the
syntax and how that works, but I can follow the logic now.

Sincerely,

Gene Wirchenko


 
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.
justaguy  
View profile  
 More options Sep 17 2012, 2:16 pm
Newsgroups: comp.lang.javascript
From: justaguy <lichunshe...@gmail.com>
Date: Mon, 17 Sep 2012 11:16:14 -0700 (PDT)
Local: Mon, Sep 17 2012 2:16 pm
Subject: Re: Question on closure
On Sep 17, 1:31 pm, Christoph Becker <cmbecke...@gmx.de> wrote:

> justaguy wrote:
> > I intend to get back on javascripting... what could be a good resource
> > to get my hands on this?
> > Yes, googling provides some interesting links...

> The c.l.js FAQ also does:
> <http://jibbering.com/faq/#ecmascriptResources> ;-)

> --
> Christoph M. Becker

Thanks.  In the meantime, it would be fun to try to create something
simple and yet useful while getting to know more about it.

 
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.
Gene Wirchenko  
View profile  
 More options Sep 17 2012, 4:56 pm
Newsgroups: comp.lang.javascript
From: Gene Wirchenko <ge...@ocis.net>
Date: Mon, 17 Sep 2012 13:56:49 -0700
Local: Mon, Sep 17 2012 4:56 pm
Subject: Re: Question on closure
On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker

<cmbecke...@gmx.de> wrote:

[snip]

>Try the following variation of JJ's code:

>function Counter() {
> var theValue = 0;
> return {
>   getVal: function() {

    ^^^^^^^^^^^^^^^^^^
     Could you please give me pointer to where this syntax is
discussed?  I tried the standard, but what I found -- sections 12.1
Block and 12.12 Labelled Statements -- is remarkably opaque.  Do I
even have the correct section(s)?  Do you know of something that
explains it better?

[snip]

Sincerely,

Gene Wirchenko


 
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.
JJ  
View profile  
 More options Sep 17 2012, 5:47 pm
Newsgroups: comp.lang.javascript
From: JJ <jaejunks_at@_googlemail_dot._com>
Date: Mon, 17 Sep 2012 21:47:18 +0000 (UTC)
Local: Mon, Sep 17 2012 5:47 pm
Subject: Re: Question on closure

Maybe this can help you understand it easier.

https://developer.mozilla.org/en-
US/docs/JavaScript/Guide/Working_with_Objects


 
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.
Stefan Weiss  
View profile  
 More options Sep 17 2012, 6:06 pm
Newsgroups: comp.lang.javascript
From: Stefan Weiss <krewech...@gmail.com>
Date: Tue, 18 Sep 2012 00:06:14 +0200
Local: Mon, Sep 17 2012 6:06 pm
Subject: Re: Question on closure
On 2012-09-17 22:56, Gene Wirchenko wrote:

> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
> <cmbecke...@gmx.de> wrote:
>>function Counter() {
>> var theValue = 0;
>> return {
>>   getVal: function() {
>     ^^^^^^^^^^^^^^^^^^
>      Could you please give me pointer to where this syntax is
> discussed?  I tried the standard, but what I found -- sections 12.1
> Block and 12.12 Labelled Statements -- is remarkably opaque.  Do I
> even have the correct section(s)?

No, this has nothing to do with labels. The return value in this case is
an object, written in the form of an object literal. I'm sure you've
seen them before:

   var obj = {
      foo: 42,
      bar: "baz"
   };

Instead of the values 42 and "baz", you can use any valid expression,
including function expressions:

   var obj = {
      foo: 42,
      bar: function () { return "baz"; }
   };

Now you can call obj.bar() to get "baz" returned.
This is equivalent to writing -

   function myFunc () {
      return "baz";
   }

   var obj = {
      foo: 42,
      bar: myFunc
   };

- except that in this case, myFunc could be called directly as well.
Putting it all together, Christoph's example did something like this:

   return {
      foo: 42,
      bar: function () { return "baz"; }
   };

HTH.

- stefan


 
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.
Gene Wirchenko  
View profile  
 More options Sep 17 2012, 10:55 pm
Newsgroups: comp.lang.javascript
From: Gene Wirchenko <ge...@ocis.net>
Date: Mon, 17 Sep 2012 19:55:53 -0700
Local: Mon, Sep 17 2012 10:55 pm
Subject: Re: Question on closure
On Tue, 18 Sep 2012 00:06:14 +0200, Stefan Weiss

     Yes.

>Instead of the values 42 and "baz", you can use any valid expression,
>including function expressions:

>   var obj = {
>      foo: 42,
>      bar: function () { return "baz"; }
>   };

     The penny drops.

     JavaScript should be called BuriedTreasure.  There are a lot of
these very neat things in it, but it can be quite the task to figure
them out.

[snip]

>HTH.

     Quite.  Thank you very much.

Sincerely,

Gene Wirchenko


 
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.
Thomas 'PointedEars' Lahn  
View profile  
 More options Sep 18 2012, 5:16 am
Newsgroups: comp.lang.javascript
Followup-To: comp.lang.javascript
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Date: Tue, 18 Sep 2012 11:14:26 +0200
Local: Tues, Sep 18 2012 5:14 am
Subject: Re: Question on closure

Stefan Weiss wrote:
> On 2012-09-17 22:56, Gene Wirchenko wrote:
>> Christoph Becker wrote:
>>> function Counter() {
>>>   var theValue = 0;
>>>   return {
>>>     getVal: function() {
>>      ^^^^^^^^^^^^^^^^^^
>>      Could you please give me pointer to where this syntax is
>> discussed?  I tried the standard, but what I found -- sections 12.1
>> Block and 12.12 Labelled Statements -- is remarkably opaque.  Do I
>> even have the correct section(s)?

> No, this has nothing to do with labels. The return value in this case is

a reference to

> an object, written in the form of an object literal. [further explanation]

ACK.

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300d...@news.demon.co.uk>


 
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.
End of messages
« Back to Discussions « Newer topic     Older topic »