Books
Books

Reputation: 163

What's wrong with this var statement declaring a variable with a dot in the name?

I'm new to JavaScript and I get an error saying that my code is missing semicolons on line 2. What semicolons does it need? I already put semicolons.

var success = function(){
    var wx.varx = $scope.vr;
    $state.go("/there");
};

Upvotes: 2

Views: 116

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075039

The problem with that line is that it's simply invalid. The error message is just the parser doing its best to figure out what's going on.

var declares a variable. Literal variable names (IdentifierName in the spec) cannot contain a ..

If you have an in-scope wx identifier referencing an object and want to set a property on it, remove var:

wx.varx = $scope.vr;

If you want to create a new variable, remove the . from the name.

var wxvarx = $scope.vr;

If you want to create a wx variable and an object containing varx as a property:

var wx = {
    varx: $scope.vr
};

Upvotes: 3

Jonas Osburg
Jonas Osburg

Reputation: 1783

You cannot declare an object property directly using

var wx.varx

As the object does not exist at this point. Instead you need to declare the object (wx) and set varx.

var vx = {
    varx: $scope.vr
};

Upvotes: 0

Deepak Kumar T P
Deepak Kumar T P

Reputation: 1076

Use like this

var wx={};
wx.varx =  $scope.vr;

You cannot use variable directly as an object

Upvotes: 0

Related Questions