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.