Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15903 > unrolled thread
| Started by | justaguy <lichunshen84@gmail.com> |
|---|---|
| First post | 2012-09-15 13:01 -0700 |
| Last post | 2012-09-17 11:16 -0700 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.javascript
Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-15 13:01 -0700
Re: Question on closure JJ <jaejunks_at@_googlemail_dot._com> - 2012-09-17 12:57 +0000
Re: Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-17 09:14 -0700
Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 10:02 -0700
Re: Question on closure Christoph Becker <cmbecker69@gmx.de> - 2012-09-17 19:23 +0200
Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 11:13 -0700
Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 13:56 -0700
Re: Question on closure JJ <jaejunks_at@_googlemail_dot._com> - 2012-09-17 21:47 +0000
Re: Question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-18 00:06 +0200
Re: Question on closure Gene Wirchenko <genew@ocis.net> - 2012-09-17 19:55 -0700
Re: Question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-18 11:14 +0200
Re: Question on closure Christoph Becker <cmbecker69@gmx.de> - 2012-09-17 19:32 +0200
Re: Question on closure justaguy <lichunshen84@gmail.com> - 2012-09-17 11:16 -0700
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-15 13:01 -0700 |
| Subject | Question on closure |
| Message-ID | <e75bb2b7-b525-44d2-a957-5cf70ef25773@z8g2000yql.googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | JJ <jaejunks_at@_googlemail_dot._com> |
|---|---|
| Date | 2012-09-17 12:57 +0000 |
| Message-ID | <XnsA0D1CBA63824Cjaejunksgooglemailco@0.0.0.50> |
| In reply to | #15903 |
justaguy <lichunshen84@gmail.com> 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...
>
> 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.
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;
}
};
})();
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-17 09:14 -0700 |
| Message-ID | <92b4d2b2-f7c3-45eb-a746-7d741cd85d22@o8g2000yqm.googlegroups.com> |
| In reply to | #15919 |
On Sep 17, 8:57 am, JJ <jaejunks_at@_googlemail_dot._com> wrote:
> justaguy <lichunshe...@gmail.com> 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...
>
> > 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.
>
> 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;
> }
> };
>
>
>
>
>
>
>
> })();
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.
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-09-17 10:02 -0700 |
| Message-ID | <2rle589b4vsrkfkdahh9f78t8vrkg69t3n@4ax.com> |
| In reply to | #15919 |
On Mon, 17 Sep 2012 12:57:37 +0000 (UTC), JJ
<jaejunks_at@_googlemail_dot._com> wrote:
[snip]
>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;
> }
> };
>})();
Could you please post the rest? How do you access the value?
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2012-09-17 19:23 +0200 |
| Message-ID | <k37m9b$1i8$1@speranza.aioe.org> |
| In reply to | #15923 |
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
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-09-17 11:13 -0700 |
| Message-ID | <90qe585o052ohdbhr68696nvgkgmhlp0nh@4ax.com> |
| In reply to | #15924 |
On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
<cmbecker69@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
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-09-17 13:56 -0700 |
| Message-ID | <v33f58pm2r4a20n2pkj3ieadccu0aauhle@4ax.com> |
| In reply to | #15924 |
On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
<cmbecker69@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
[toc] | [prev] | [next] | [standalone]
| From | JJ <jaejunks_at@_googlemail_dot._com> |
|---|---|
| Date | 2012-09-17 21:47 +0000 |
| Message-ID | <XnsA0D23131E8AC0jaejunksgooglemailco@0.0.0.50> |
| In reply to | #15932 |
Gene Wirchenko <genew@ocis.net> wrote:
> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
> <cmbecker69@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
Maybe this can help you understand it easier.
https://developer.mozilla.org/en-
US/docs/JavaScript/Guide/Working_with_Objects
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2012-09-18 00:06 +0200 |
| Message-ID | <k386sm$m64$1@news.albasani.net> |
| In reply to | #15932 |
On 2012-09-17 22:56, Gene Wirchenko wrote:
> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
> <cmbecker69@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
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-09-17 19:55 -0700 |
| Message-ID | <1hof581tj41e5ia2kdubti7hsj4ueqn1if@4ax.com> |
| In reply to | #15935 |
On Tue, 18 Sep 2012 00:06:14 +0200, Stefan Weiss
<krewecherl@gmail.com> wrote:
>On 2012-09-17 22:56, Gene Wirchenko wrote:
>> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
>> <cmbecker69@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"
> };
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-09-18 11:14 +0200 |
| Message-ID | <9918278.jPXTlJuZSa@PointedEars.de> |
| In reply to | #15935 |
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$8300dec7@news.demon.co.uk>
[toc] | [prev] | [next] | [standalone]
| From | Christoph Becker <cmbecker69@gmx.de> |
|---|---|
| Date | 2012-09-17 19:32 +0200 |
| Message-ID | <k37mq7$31h$1@speranza.aioe.org> |
| In reply to | #15903 |
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
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-17 11:16 -0700 |
| Message-ID | <12e1fae8-1c8d-4af6-82e7-6f795253f6d2@n9g2000yqn.googlegroups.com> |
| In reply to | #15925 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web