无效的分配左侧,javascript

时间:2011-06-28 08:04:08

标签: javascript variable-assignment

我确信我在做一些愚蠢的事情:

var addhtml = '<div id="leftbio" class="left-float">'
+= '<div id="bioname">e["screen_name]</div>'
+= '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
+= '<div id="biodetails">e["description"]</div>'
+= '</div>';             // invalid assignment left-hand side
console.log(addhtml);

Netbeans告诉我invalid assignment left-hand side错误。

怎么了?

6 个答案:

答案 0 :(得分:13)

您不需要+=进行连接,只需要+

这没关系

var addhtml = '<div id="leftbio" class="left-float">'
+ '<div id="bioname">e["screen_name]</div>'
+ '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
+'<div id="biodetails">e["description"]</div>'
+ '</div>';         
console.log(addhtml);

答案 1 :(得分:9)

+=表示“将事物放在左边,将其添加到其中,然后将结果存储在左侧的事物中”。 +=的左侧是文字(第一个是'<div id="leftbio" class="left-float">)。你不能分配文字。

换句话说,a += b基本上意味着a = a + b。如果a是文字而不是变量,您可以看到这不起作用。

你只需要+

var addhtml = '<div id="leftbio" class="left-float">'
+ '<div id="bioname">e["screen_name]</div>'
+ '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
+ '<div id="biodetails">e["description"]</div>'
+ '</div>';
console.log(addhtml);

让您了解++=之间的区别:

var a, b;
a = "foo";
b = a + "bar";  // Doesn't modify `a`
console.log(a); // "foo"
console.log(b); // "foobar"

VS

var a, b;
a = "foo";
b = a += "bar"; // Modifies `a` (assigning the result to `b` is unusual -- very -- but valid)
console.log(a); // "foobar" - note it's changed
console.log(b); // "foobar"

关闭-主题

我还建议缩进赋值语句的后续行,但这只是样式:

var addhtml = '<div id="leftbio" class="left-float">'
    + '<div id="bioname">e["screen_name]</div>'
    + '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
    + '<div id="biodetails">e["description"]</div>'
    + '</div>';
console.log(addhtml);

答案 2 :(得分:2)

分配(=)不是必需的,你可以使用+。还有另外两种构建多行字符串的方法:

// method 1: use continuation \
 var addhtml = '\
        <div id="leftbio" class="left-float"> \
            <div id="bioname">e["screen_name]</div> \
            <div id="biophoto"><img src="e["profile_image_url"]"/></div> \
            <div id="biodetails">e["description"]</div> \
        </div>';

//method 2: use an array and join the elements
 var addhtml = [
       '<div id="leftbio" class="left-float">',
       ' <div id="bioname">e["screen_name]</div>',
       ' <div id="biophoto"><img src="e["profile_image_url"]"/></div>',
       ' <div id="biodetails">e["description"]</div>',
       '</div>'
     ].join('');

答案 3 :(得分:1)

x += yx = x + y的简写,这不是您想要的。

使用:

var addhtml = '<div id="leftbio" class="left-float">';
addhtml += '<div id="bioname">e["screen_name]</div>';
addhtml += '<div id="biophoto"><img src="e["profile_image_url"]"/></div>';
addhtml += '<div id="biodetails">e["description"]</div>';
addhtml += '</div>';

或:

var addhtml = '<div id="leftbio" class="left-float">'
    + '<div id="bioname">e["screen_name]</div>'
    + '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
    + '<div id="biodetails">e["description"]</div>'
    + '</div>';

答案 4 :(得分:0)

不需要=

var addhtml = '<div id="leftbio" class="left-float">'
    + '<div id="bioname">e["screen_name]</div>'
    + '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
    + '<div id="biodetails">e["description"]</div>'
    + '</div>';             // invalid assignment left-hand side
    console.log(addhtml);

答案 5 :(得分:0)

您无法将+=等归因运算符链接起来。

var addhtml = '<div id="leftbio" class="left-float">'
              + '<div id="bioname">e["screen_name]</div>'
              + '<div id="biophoto"><img src="e["profile_image_url"]"/></div>'
              + '<div id="biodetails">e["description"]</div>'
              + '</div>';             // invalid assignment left-hand side
console.log(addhtml);