Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16143

Re: silly question on closure

Message-ID <1779068.4r8zmURcku@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-09-24 20:49 +0200
Subject Re: silly question on closure
Newsgroups comp.lang.javascript
References (1 earlier) <505dfcdb$0$6848$e4fe514c@news2.news.xs4all.nl> <02f07c90-fde4-4cc6-b44b-95239771c06d@m9g2000yqi.googlegroups.com> <7008a319-a3c7-43b4-9a05-dd1db287472f@l14g2000yqo.googlegroups.com> <k3nejp$6v3$1@news.albasani.net> <0c5aab04-4fe4-41f8-b50f-ec9ea4821905@e18g2000yqo.googlegroups.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


justaguy wrote:

> On Sep 23, 12:50 pm, Stefan Weiss <krewech...@gmail.com> wrote:
>> On 2012-09-23 16:31, justaguy wrote:
>> > Ok, I've implemented closure with my app, and now I see its beauty, in
>> > the meantime, I don't know why
>> > ++value;
>> > would increment by 2.
>> > I attempted with
>> > +value to no avail
>>
>> The ++ operator increments by 1, not 2.
>> The unary + operator does not increment at all.
>>
>> This is basic syntax. I think you would profit enormously from reading
>> this guide:
>>
>> https://developer.mozilla.org/en-US/docs/JavaScript/Guide
>>
>> Chapter 5 is about expressions and operators, chapter 14 is about
>> closures.
> 
> Ok, back the my latest question/post, I guess I was almost correct,
> manyCars are considered an object-like property, and it needs the "
> like the following,
> console.log(car["manyCars"]); // a: "Saab", "b": "Jeep"

You are not making any sense.  There are no "object-like properties" (there 
can be *array*-like *objects*, a completely different thing); these 
languages are loosely typed.  Getters, setters and freezing (ES5) aside, any 
property can assume any value at any time.  And a property's *value* is 
irrelevant to the *syntax* the property can be accessed with¹:

  console.log(car.manyCars);

works as well, of course.  The only case where the bracket accessor notation 
is required is when the property name is not an identifier, including when 
it is a reserved word.¹  (Whereas the latter has been fixed with ES 5, so 
one can hope that some day – perhaps 10 years from now – non-ES-5-conforming 
engines have gone.)

What does not work well if `manyCars' is a reference to an object, is

  console.log(car[manyCars]);

because property names are string values, so the value of `manyCars' would 
be converted to a string value there if it is not already is one.  And for 
object references that means the toString() method of the object is called, 
which for built-in objects returns just "[object Object]".  And there is no 
property with *that* name in your program.  (JSX:map.js allows you to use 
objects as keys, refining the concept underlying jshashtable by Tim Down.)

If you want to have any hope of understanding these languages (yes, it is 
not only one) some day, you should read a lot more carefully because I doubt 
anything in the JavaScript Guide suggests what you stated here.  And you 
should stop posting via Google Groups using a pseudonym, because the people 
you want to be corrected by do not have the time to filter out your posting 
from all the other junk that is usually posted via Google Groups and by 
pseudonymous posters to Usenet.  I am making an exception for this posting, 
of course.


PointedEars
___________
¹ And by convention and for clarity of code, it is useful when accessing
  DOM collections with ECMAScript implementations.
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 05:04 -0700
  Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-22 18:18 +0200
    Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:41 -0700
    Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:10 +0200
      Closures (was: silly question on closure) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 13:14 +0200
      Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 05:04 +0200
        Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-27 20:50 +0200
          Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 21:29 +0200
  Re: silly question on closure "Tom de Neef" <tdeneef@qolor.nl> - 2012-09-22 20:00 +0200
    Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:42 -0700
      Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 07:31 -0700
        Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-23 18:49 +0200
          Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:33 -0700
          Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:38 -0700
            Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 20:49 +0200
    Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:14 +0200
  Re: silly question on closure dann90038@gmail.com - 2012-09-23 15:37 -0700

csiph-web