JavaScript实验:在代码中分多行定义字符串

在JavaScript中,如果某个字符串过长,可通过以下2种方式来分行进行定义:

第一种,通过+操作符来拼接字符串。比如:

var s = "The First Part " +  
        "The Second Part " +
        "The Third Part";

第二种,通过斜线()来分隔位于不同行的字符串。比如:

var s = "The First Part\  
         The Second Part\
         The Third Part";

根据Google的JavaScript编程规范,不推荐使用分行定义字符串的行为;如果确实需要分行定义字符串,那么应该采取上述第一种方式来进行。

实验

var s = "The First Part " +  
        "The Second Part " +
        "The Third Part";
console.log(s);  

上述代码的打印结果为:“The First Part The Second Part The Third Part”。

Chuan Shao

Read more posts by this author.

Shanghai

Subscribe to Chuan's blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!