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.
> 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.
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?
>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.
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...
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...
^^^^^^^^^^^^^^^^^^
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?
Gene Wirchenko <ge...@ocis.net> wrote:
> 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?
> 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"; }
};
<krewech...@gmail.com> wrote:
>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"
> };
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.
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>