2

How comes this works:

function Test() {
    this.t=function() {
        var self=this;
        self.tutu = 15;
        console.log(self);
    }
}
var olivier = new Test();

This works:

function Test() {
    this.t=function() {
        var self  = this,
            other = -1;
        self.tutu = 15;
        console.log(self);
    }
}
var olivier = new Test();

And this doesn't work (with the error SyntaxError: Unexpected token .):

function Test() {
    this.t=function() {
        var self  = this,
            other = -1,
            self.tutu = 15;
        console.log(self);
    }
}
var olivier = new Test();
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

4 Answers4

2

var statement is used to declare variables. So, you are trying to define a variable with name self.tutu, which is not valid in JavaScript, as variable names should not have . in their names. That is why it is failing with Syntax error.

SyntaxError: Unexpected token .

Quoting from Variables section in MDN,

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the \uXXXX Unicode escape sequences as characters in identifiers.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • If I try `self['tutu']` it's the same problem. – Olivier Pons Apr 29 '14 at 07:31
  • @OlivierPons Correct, because `[`, `'` and `]` are also not allowed in variable declarations. – thefourtheye Apr 29 '14 at 07:32
  • @OlivierPons In other two examples, you are assigning to a property of an object, but in the last example you are trying to create a variable with invalid characters in the identifier – thefourtheye Apr 29 '14 at 07:33
  • Try this: `var a={ t: 15}, b=a.t;` and this: `var a={ t: 15}, b=a['t'];`. It works for me with Chrome – Olivier Pons Apr 29 '14 at 07:35
  • @OlivierPons In this case, variable names are just `a` and `b`. Values can be anything. For example, try `var a={t: 0}, a.t = 15;`, it will fail. Because it has a problem with the variable name `a.t` – thefourtheye Apr 29 '14 at 07:37
  • Aaaaaaaaah I understand what you mean. Thank you very much for this clean explanation. – Olivier Pons Apr 29 '14 at 07:40
1

var could only used to declare variables, but not before expression.

var self.tutu = 15; is not valid.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

The last pattern doesn't work because you are creating a property of self within the variable declaration block. You could rewrite your code to:

var self = (this.tutu = 15, this),
           other = -1;
/* self = (this.tutu = 15, this) =>
   (,) creates a group. Using the comma operator,
   the statements within the group are evaluated from
   left to right. After evaluation self is a reference to this
   and this now also contains the property tutu */
KooiInc
  • 119,216
  • 31
  • 141
  • 177
1

Pretty similar to this: Multiple left-hand assignment with JavaScript

Per that answer, you're actually doing this: var self = (window.other = (self.tutu = 15)), which of course will give the SyntaxError, because you're trying to assign self.tutu before self exists.

I'm not sure there's a way to do parallel assignment in this way, but of course

var self = this;
var other = -1;
self.tutu = 15;

will work fine.

Community
  • 1
  • 1
gdpelican
  • 568
  • 4
  • 12
  • I'm not trying to assign "self.tutu before self exists.". In Javascript, you can do this: `var a={ t: 15}, b=a.t;` and it works flawlessly, which is pretty similar to what I'm asking. – Olivier Pons Apr 29 '14 at 07:33