473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Identifying language concepts from sample code

I have the Javascript definitive guide book but I'm hoping someone can
tell me what "language concepts" are being employed by the following
code, so that I can look up the right parts in the book:

foo = {
attrType : 'none',
init : function(){ ..... snip...

and ...

var foo = {
attrType : 'none',
init : function(){ ... snip ......
foo = function () {
var bar;
var grid;
var colModel; ..... snip...

The first statement looks, to me, like a class definition and so to
create a foo, I would need to declare a variable of type foo, is that
right? The second case looks similar to the first declaration only it
would appear that a global foo variable is actually created.. and I've
no idea what the last one does. Basically it baffles me a bit that
"var" has been left out of the 1st and 3rd example but they all appear
to me to be examples of associative arrays... but I'm guessing just from
reading the first few chapters of the book.

Anyway if someone could tell me how these declarations differ in their
utility and what "concepts" they come under, I can then go and google etc.

Ta.

Bob.
Nov 9 '06 #1
3 1533
User1013 wrote:
I have the Javascript definitive guide book but I'm hoping someone can
tell me what "language concepts" are being employed by the following
code, so that I can look up the right parts in the book:

foo = {
attrType : 'none',
init : function(){ ..... snip...
'Assignment', and 'object literal/initialiser' (including a 'function
expression').
and ...

var foo = {
attrType : 'none',
init : function(){ ... snip ......
'Variable declaration', 'assignment', and 'object literal/initialiser'
(including a 'function expression').
foo = function () {
var bar;
var grid;
var colModel; ..... snip...
A 'function expression', with the resulting value (a reference to a
function object) being assigned to an Identifier.
The first statement looks, to me, like a class definition
There are no 'classes' in javascript (or there is precisely one class,
depending on how you look at it). In javascript 'class' concepts are
(or may be) imposed by the programmer and are not part of the language.
and so to create a foo,
Object literals/intialisers create and define an instance of the
ECMAScript native object (the only object type in javascript). The code
above assigns a reference to that one object to the Identifier - foo -.
I would need to declare a variable of type foo,
Javascript is loosely typed so you don't declare variables of any type,
and there is no "type foo". All variables and object properties may be
assigned values of any type. It is the programmers responsibility to
know/track what type(s) they are using.
is that right?
Not so far.
The second case looks similar to the first declaration
Virtually Identical, and practically identical if the code is executing
in the global execution context.
only it would appear that a global foo variable is actually created.
In that case - foo - is declared. That declaration results in a
property being created on the execution context's Variable/Activation
object. If the code is executed in a function expectation context then
the result is a local variable, if executed in the global execution
context the Activation/Variable object is the global object. An
assignment to an undeclared Identifier, where no object on the scope
chain (or their prototypes) has (have) a property with the
corresponding name results in the creation of a property of the global
object. Thus an assignment to an undeclared Identifier tends to
resemble the runtime creation of a global variable, and will have the
same effect in any execution context. (There is a slight distinction in
that a declared variable cannot be deleted, but a runtime created
property of the global object can be).
>. and I've
no idea what the last one does.
It creates a function object as the function expression is evaluated,
and then assigns the value of that function object (a reference to the
function object) to the Identifier - foo -.
Basically it baffles me a bit that
"var" has been left out of the 1st and 3rd example but they all appear
to me to be examples of associative arrays...
There are no 'associative arrays' in javascript, there is a single
object type to which named properties can be added at runtime and
values assigned to those properties (that may resemble 'associative
arrays' or hashtables, but there are some very significant dictions,
such as it being impossible to create an object that has no existing
properties, thus they are never 'empty').
but I'm guessing just from
reading the first few chapters of the book.
Chapter 8 covers functions, and should cover function expression,
though it may not use the correct terms.
Anyway if someone could tell me how these declarations differ in their
utility
How they differ in their utility? That is a huge subject, and very
context related. For example, generally assigning to an undeclared
Identifier would be bad practice, but the wider context may make it
completely sensible for the above code to be doing so.
and what "concepts" they come under, I can then go and google etc.
Richard.

Nov 9 '06 #2
Lee
User1013 said:
>
I have the Javascript definitive guide book but I'm hoping someone can
tell me what "language concepts" are being employed by the following
code, so that I can look up the right parts in the book:

foo = {
attrType : 'none',
init : function(){ ..... snip...

and ...

var foo = {
attrType : 'none',
init : function(){ ... snip ......
foo = function () {
var bar;
var grid;
var colModel; ..... snip...

The first statement looks, to me, like a class definition and so to
create a foo, I would need to declare a variable of type foo, is that
right?
No. It's creating an object variable named "foo".
It has attributes named attrType and init. The second attribute
happens to be a function. You might find this indexed as an
"object literal".
>The second case looks similar to the first declaration only it
would appear that a global foo variable is actually created.
The only difference is that, if this is declared inside a function,
it will be local to that function. The "var" keyword means "local".
>and I've no idea what the last one does.
It's the beginning of a function definition in which local
variables are declared. It's equivalent to:
function foo() { ...
>Basically it baffles me a bit that
"var" has been left out of the 1st and 3rd example but they all appear
to me to be examples of associative arrays... but I'm guessing just from
reading the first few chapters of the book.
Objects are sometimes used as if they were associative arrays
which happen to include "keys" that you didn't put there.
--

Nov 9 '06 #3
* Richard Cornford wrote:
User1013 wrote:

A 'function expression', with the resulting value (a reference to a
function object) being assigned to an Identifier.
>The first statement looks, to me, like a class definition

There are no 'classes' in javascript (or there is precisely one class,
depending on how you look at it). In javascript 'class' concepts are
(or may be) imposed by the programmer and are not part of the language.
.... (snip)

Thank you very much for your reply, I shall now delve into my book this
weekend try and reverse engineer the javascript.

Thanks once again and have a good weekend :-)
Nov 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Greg F. | last post by:
Dear Folks: Question #1: I have not been able to find a method to acknowledge or indicate that a form is open (not necessarily have the focus). Any ideas on how to check if a particular form...
4
by: Marshal | last post by:
I think one of the major advancements on C# will be the growing support for Extension Methods and related concepts... The current syntax involves a static class with static members using a...
18
by: Jeff S | last post by:
It seems that anyone can put anything on their resume and refer to themselves as a "senior Web developer." What are some clues that can be used by an IT manager during a hiring process to...
27
by: hacker1017 | last post by:
im just asking out of curiosity.
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: PHPBob | last post by:
Hello, I am trying to run Languid language guesser - see http://languid.cantbedone.org/ I am not a perl programmer, and I have no idea how to get this script running. I am running perl scripts...
12
by: Tony Belding | last post by:
I'm interested in using an off-the-shelf interpreted language as a user-accessible scripting language for a MUCK. I'm just not sure if I can find one that does everything I need. The MUCK must be...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
43
by: Adem24 | last post by:
The World Joint Programming Language Standardization Committe (WJPLSC) hereby proclaims to the people of the world that a new programming language is needed for the benefit of the whole mankind in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.