Marvin said:
Vic Sowers wrote:
Huh?????
He meant
var myObj = ...
or "referenced as MyObj.a".
BTW, your Question Mark key is borken.
<snip>
Sorry, I should have phrased my question as follows.....
What is the difference between the following:
myFunc(x) { a = 1; }
myFunc(x) { var a = 1; }
myFunc(x) { this.a = 1; }
myFunc(x) { a : 1; }
The difference is different code. The lines have in common that neither
one is syntactically correct, since the `function' keyword is missing.
I understand the last one.
Are you sure?
In the 1st, I take it that "a" is defined as a local variable.
(Let us assume in the following for brevity that each of the above
lines is prefixed with `function '.)
No, it is not. Since in there is no variable `a' declared in that
execution context, you are adding a new property to the next object
in the scope chain, usually the Global Object (but there are known
side effects with host objects, e.g. in IE). This would be similar
to defining a global variable.
The difference between the behavior here and the behavior with the
code in your previous posting is that there was a VariableDeclaration
for `a' within the same execution context. Hence the first statement
did refer to the next object in the scope chain that had such a property,
which was the VariableObject of the current execution context (and
therefore `a' referred to the local variable. Even though the
VariableStatement followed the assignment, it was parsed first;
the initialization, however, happened later, after the unqualified
assignment.)
Variables should always be declared, or they are no variables attached
to an execution context, but properties of any object in the scope chain.
In the 2nd, "a" is a global variable.
No, it is not. Because the `var' keyword (a VariableStatement) was used,
a new property of the Variable Object of the current execution context
was created. Therefore, a local variable was declared. References to
`a' within this execution context refer to that variable, not to the
(global) property that may have been created before (by calling the first
version of myFunc()).
But I am still confused about "this.a". I am especially confused about the
differences between:
x = myFunc(arg);
Provided that `arg' exists (which I will assume from now for brevity), if
myFunc() is called like this, it is called as a method of the Global Object
(unless there is another object in the scope chain before the Global
Object, see above). Therefore, `this' within myFunc() refers to that
object.
If myFunc() is used as a constructor as called in a NewExpression like here,
`this' within myFunc() refers to the object that is constructed. (That
object inherits directly from myFunc.prototype, and is therefore called a
"myFunc object".)
This does not call myFunc(), because it is not a CallExpression (the
argument list is missing) or a NewExpression (there is no prefixed `new'
keyword). `x' is assigned the value of myFunc, which is a reference to a
Function object. Therefore, after that line, x() can be [[Call]]ed and
[[Construct]]ed as if myFunc() was [[Call]]ed or [[Construct]]ed. It is
is not copying an object, it is copying the reference to that object.
[...]
Also, is this:
o = {a:1, b:2, c:3};
the same as the following?
o = new Object;
o.a =1;
o.b = 2;
o.c = 3;
It is. An Object literal is both a constructor and an initializer,
so is an Array literal [1, 2, 3], and a RegExp literal /1+2*3/. The
difference with the RegExp literal is that the corresponding RegExp
object is created before execution, and so is only created once, even
if it is used several times in the code.
This is very basic knowledge (no matter the terms used to describe
it). Please search the archives, RTFAQ[1] RTFM, and STFW, before
you post again.
HTH
PointedEars
___________
[1] <URL:
http://jibbering.com/faq/>