简体   繁体   English

JavaScript分配自变量

[英]JavaScript assigning self variable

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 . ): 而且这不起作用 (出现错误SyntaxError: Unexpected token . ):

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

var statement is used to declare variables. var语句用于声明变量。 So, you are trying to define a variable with name self.tutu , which is not valid in JavaScript, as variable names should not have . 因此,您正在尝试定义名称为self.tutu的变量,该变量在JavaScript中无效,因为变量名不应该具有. in their names. 以他们的名字。 That is why it is failing with Syntax error. 这就是为什么它因语法错误而失败。

SyntaxError: Unexpected token .

Quoting from Variables section in MDN , MDN中的“变量”部分报价,

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); JavaScript标识符必须以字母,下划线(_)或美元符号($)开头; subsequent characters can also be digits (0-9). 后续字符也可以是数字(0-9)。 Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). 因为JavaScript区分大小写,所以字母包括字符“ A”至“ Z”(大写)和字符“ a”至“ z”(小写)。

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. 从JavaScript 1.5开始,您可以在标识符中使用ISO 8859-1或Unicode字母,例如å和ü。 You can also use the \\uXXXX Unicode escape sequences as characters in identifiers. 您也可以使用\\ uXXXX Unicode转义序列作为标识符中的字符。

var could only used to declare variables, but not before expression. var只能用于声明变量,而不能用于表达式之前。

var self.tutu = 15; is not valid. 无效。

The last pattern doesn't work because you are creating a property of self within the variable declaration block. 最后一种模式不起作用,因为您正在变量声明块内创建self属性。 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 */

Pretty similar to this: Multiple left-hand assignment with JavaScript 与此非常相似: 使用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. 根据该答案,您实际上正在执行此操作: var self = (window.other = (self.tutu = 15)) ,这当然会产生SyntaxError,因为您正在尝试在self存在之前分配self.tutu

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. 会很好的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM