Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Clarifying scope of literals

Reply
Thread Tools

Clarifying scope of literals

 
 
ociardhp
Guest
Posts: n/a
 
      10-25-2007
Hi

I've seen some code similar to the following and would like to confirm
my understanding. I assume the object literal created in the return
has access to the enclosing scope which includes the variable age,
effectively creating a closure over it when created, and that this is
what enables the assignment to moon.age? Is this considered good style
and is there any particular reason for using that approach as opposed
to the clearer (in my opinion) declaration of age within the object
literal?

Many thanks

Paul

<html>
<body>
<div id="holder"></div>
<script>

moon = function() {
var age;

return {
// age: 4000000000,

showDarkSide: function() {
return false;
}
}
}();

moon.age = 42;

document.getElementById("holder").innerHTML = "Age: " + moon.age +
"<br/> ShowDarkSide: " + moon.showDarkSide();

</script>
</body>
</html>

 
Reply With Quote
 
 
 
 
David Golightly
Guest
Posts: n/a
 
      10-25-2007
On Oct 25, 11:29 am, ociardhp <paul.p.ca...@gmail.com> wrote:
> I've seen some code similar to the following and would like to confirm
> my understanding. I assume the object literal created in the return
> has access to the enclosing scope which includes the variable age,
> effectively creating a closure over it when created, and that this is
> what enables the assignment to moon.age? Is this considered good style
> and is there any particular reason for using that approach as opposed
> to the clearer (in my opinion) declaration of age within the object
> literal?


<snip>

> <html>
> <body>


<snip>

> <script>
> moon = function() {
> var age;
>
> return {
> // age: 4000000000,
>
> showDarkSide: function() {
> return false;
> }
> }
> }();
>
> moon.age = 42;
>
> document.getElementById("holder").innerHTML = "Age: " + moon.age +
> "<br/> ShowDarkSide: " + moon.showDarkSide();



Couple of points to start:
1. using <script> tags in the <body> of the document is nonstandard
and should be avoided.
2. you should always use the "var" keyword to declare your variables
("moon" in your example), and it's a good practice to automatically do
this, even if the variable is global.

There, now to your question.

One thing that's perhaps confusing in the discussion of closure and
scope in JavaScript is that scope is much simpler than you would think
from a classical OOP language. Your confusion probably comes from mis-
associating a concept of "private" variables in JavaScript. JS
doesn't have a "private" concept. Your example effectively creates
two distinct "things" with the name "age": one is a variable that is
local to the anonymous function that returns the object that gets
assigned to "moon"; this object literal has no "age" property until
you assign it on the next line ("moon.age = 42"). Since "moon" is an
object, you can always assign values to properties on the object -
there's no such thing as a "private" property in JavaScript. But the
"age" property you've created through assignment shouldn't be confused
with the "age" local variable in the anonymous function's scope.

Remove that line assigning a value to "moon.age" and re-run your code;
you'll see what actually happens.

-David

 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      10-25-2007
On Oct 25, 2:41 pm, David Golightly <davig...@gmail.com> wrote:
[snip]
> 1. using <script> tags in the <body> of the document is nonstandard
> and should be avoided.


Non-standard according to who?

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-25-2007
David Golightly wrote:
> Couple of points to start: 1. using <script> tags in the <body> of the
> document is nonstandard and should be avoided.


Utter nonsense.

> 2. you should always use the "var" keyword to declare your variables
> ("moon" in your example), and it's a good practice to automatically do
> this, even if the variable is global.


If the identifier is not declared, the assignment it will end up as an
assignment to an enumerable property of an object in the scope chain.
That does not have to be the Global Object, hence this recommendation.


PointedEars
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      10-26-2007
On Oct 26, 4:29 am, ociardhp <paul.p.ca...@gmail.com> wrote:
> Hi
>
> I've seen some code similar to the following and would like to confirm
> my understanding. I assume the object literal created in the return
> has access to the enclosing scope which includes the variable age,
> effectively creating a closure over it when created, and that this is
> what enables the assignment to moon.age?


No (see below).


> Is this considered good style
> and is there any particular reason for using that approach as opposed
> to the clearer (in my opinion) declaration of age within the object
> literal?


The pattern is used to emulate the behaviour of private variables that
can be read or set by priveliged functions:

<URL: http://www.crockford.com/javascript/private.html >


[...]
> <script>
> moon = function() {
> var age;


This variable is local to the anonymous function assigned to moon. It
can't be directly accessed from outside the function.


>
> return {
> // age: 4000000000,


Had you not commented out that line, the object returned by the
function and assigned to moon would have an age property with a value
of 4000000000. It is a different thing to the local age variable
declared earlier - they both exist independently.


> showDarkSide: function() {
> return false;
> }


This function has access to the activation object created by executing
the "outer" anonymous function, so you could make it a priveliged
function that gets or sets the value of the private age variable. It
can also modify the public moon.age property.


> }
> }();
>
> moon.age = 42;


Here you create a property of moon called age (if it doesn't already
exist) and assign it a value of 42.


--
Rob

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-26-2007
David Golightly a écrit :
(snip)
> 1. using <script> tags in the <body> of the document is nonstandard
> and should be avoided.


You may want to check the standard before asserting such a thing:

"""
The SCRIPT element places a script within a document. This element may
appear any number of times in the HEAD or BODY of an HTML document.
"""
http://www.w3.org/TR/html4/interact/...ml#edef-SCRIPT


 
Reply With Quote
 
ociardhp
Guest
Posts: n/a
 
      10-26-2007
Many thanks for all the informative replies.
Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need some help clarifying issues with YAML and hashes Frank Church Ruby 1 06-26-2007 01:03 PM
Need help clarifying use of VBxxx for creating webpages ... brooksr ASP General 10 12-26-2006 07:43 PM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Need help clarifying SMI vs EMI on Catalyst 4507R and 3750G and comments on my config Ned Cisco 2 05-25-2005 11:43 AM
Clarifying my earlier post about SIMULATION questions Kurt Hudson MCSA 0 04-18-2005 06:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57