ES6 new syntax of Literal

本文介绍了ES6中对象字面量的新特性,包括属性和方法的简洁语法、动态属性名及模板字符串的使用。展示了如何简化对象创建过程,并通过实例演示了模板字符串的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Object Literal Extensions.md

var price = 4.20, quantity = 20;
var invoiceData = {
    price: price,
    quantity: quantity
};

console.log(invoiceData);

1179506-20180425084113498-580864225.png

Declaring price and quantity twice is kind of redundant,but es6 provides a shorthand making writing this simpler.

var price = 4.20, quantity = 20;
var invoiceData = {
    price,
    quantity
};

console.log(invoiceData);

1179506-20180425093411449-391921461.png

ES6 also gives us a short notation to wirte functions in an object literal.

const price = 4.20,quantity = 20;
const invoiceData = {
    price,
    quantity,
    calculateTotal(){
        return this.price * this.quantity;
    }
};

console.log(invoiceData.calculateTotal());

1179506-20180425094012139-212455888.png

In the above shorthand notation, you no longer need the keyword function.When we use the shorthand within an object literal, this refers to the context of the code just like an arrow funtion.It does not refer to the object that contains the function.It behaves exactly like an arrow function.

const field = "dynmicRandom";
const price = 5.99;
const quantity = 2;
const invoiceData = {
    [field]:price,
    quantity,
    calculateTotal(){
        return this.price * this.quantity;
    }
};

console.log(invoiceData);

1179506-20180425094148478-1390626400.png

const field = "dynmicRandom";
const price = 5.99, quantity = 2;
const invoiceData = {
    [field + "-01"]: price,
    quantity,
    calculateTotal(){
        return this.price * this.quantity;
    }
};
console.log(invoiceData);

1179506-20180425094246797-1129977843.png

Template Literals and Delimiters

ES6 Template Literals which provide you a way to defined strings with addational functionalities like
String interpolation
Embedded expressions
Multiline strings without hacks
String formatting

let user = `Kevin`;
console.log(`Hi ${user}!`);
let a = 10;
let b = 20;
console.log(`Sum of ${a} and ${b} is ${a+b}`);
console.log(`I am line one
    I am line two`);
output `Hi, my name is ${name} and I love ${language}`;

1.Array of literal strings, that is ["Hi,my name is ","and I love", ""]
2.Substitutions,that is, name, language.

output(["Hi,my name is", "and I love", ""],name, language)

function output(literals, ...substitutions){
    let result= "";
    for(let i = 0 ;i < substitutions.length; i++) {
        result += literals[i] + substitutions[i];
    }

    result += literals[literals.length - 1];
    return result;
}
const name = "John",
    language = "Javascript";

    let text = output `Hi,my name is ${name} and I love {language}`
    console.log(text);

Iterating with for...of

let names = ['matt','smith','jack'];

for(let i = 0; i<names.length; i++) {
    console.log(name[i]);
}
let names = ["matt", "smith", "jack"];

for(let names of names){
    console.log(name);
}
for(let char of "Bye"){
    console.log(char);
}

转载于:https://www.cnblogs.com/InnerPeace-Hecdi/p/8939656.html

<think>好的,我现在需要帮用户解决一个 SyntaxError 的问题,错误信息是“string literal contains an unescaped line break”,出现在 MongoDB 的查询语句中。用户给出的代码是: db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } }) 首先,我得分析这个错误的原因。SyntaxError 通常意味着语法错误,而错误信息明确提到了字符串字面量中有未转义的换行符。不过这里用户使用的是 MongoDB 的 shell 命令,看起来像是一个 JavaScript 对象,可能是在某些上下文中被当作字符串处理了? 用户提到错误位置是 @(shellhelp1):1:53,这说明问题出在代码的第1行第53个字符附近。我需要检查用户的代码是否存在换行符没有正确转义的情况。 首先,观察用户的查询结构。MongoDB 的 find 方法接受一个查询文档,这里使用的是 roomId 的 $in 操作符,数组中的值都是数值类型,看起来没有问题。语法结构正确,没有明显的拼写错误。 不过,用户可能在某个环境中将这段代码作为字符串传递,比如在命令行中直接执行时,或者在某种脚本中未正确处理多行字符串。例如,在 shell 脚本中,如果没有使用合适的引号或转义,换行符会导致问题。 在 JavaScript 或 MongoDB shell 中,多行对象是允许的,因为 shell 会自动处理。但如果在其他环境,比如在字符串中使用时,换行符需要被转义。例如,在 JavaScript 中,如果这段代码被包裹在单引号或双引号中,换行符必须用反斜杠转义或者使用模板字符串。 假设用户是在某种需要字符串的上下文中执行这段代码,比如通过 eval 或者某种脚本工具,那么换行符确实会导致字符串中断,从而引发语法错误。这时候需要将整个查询写成一行,或者使用转义字符。 可能的解决方案包括: 1. 将查询写成一行,避免换行。 2. 在多行字符串中使用反斜杠转义换行符。 3. 使用模板字符串(如ES6的反引号)来包裹多行字符串。 例如,在 JavaScript 中,如果作为字符串: const query = "db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } })"; 或者使用反斜杠: const query = "db.liveWatchRecordDat.find({\ roomId: { $in: [10000484, 10000523, 10001044, 10001113] }\ })"; 或者使用反引号: const query = `db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } })`; 另外,检查用户是否在命令行中直接粘贴多行命令时没有正确使用续行符。在 MongoDB shell 中,多行命令是允许的,但可能需要适当的括号匹配,或者用户可能意外地在字符串中包含了换行符。 例如,如果用户是在 shell 中直接输入这段代码,应该没有问题,因为 MongoDB shell 支持多行输入。但如果是通过某种方式将代码作为字符串传递,比如在脚本中,那么换行符需要处理。 可能用户的环境或工具导致代码被解析为字符串,从而引发换行问题。需要根据具体情况调整字符串的格式,确保换行符被正确转义或避免换行。 总结,错误的原因是代码中的换行符在字符串字面量中没有正确转义,解决方案是调整代码格式,使其符合所在环境的字符串处理规则。</think>出现 `SyntaxError: "" string literal contains an unescaped line break` 错误的原因是:**你的 MongoDB 查询语句中存在未转义的换行符**,导致 JavaScript 引擎(或 MongoDB Shell)认为字符串被意外中断。 --- ### 错误分析 你的代码: ```javascript db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } }) ``` 虽然语法本身是正确的,但问题可能出现在以下场景: 1. **代码被包裹在引号中**(如作为字符串传递给 `eval()` 或某些脚本工具)。 2. **直接粘贴到某些不支持多行输入的 Shell 环境**(如部分在线工具或非交互式 Shell)。 --- ### 解决方案 #### 方法 1:写成单行(推荐) **直接删除换行符**,让代码保持为一行: ```javascript db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } }) ``` #### 方法 2:转义换行符(兼容性更好) 在换行处添加反斜杠 `\` 显式转义: ```javascript db.liveWatchRecordDat.find({\ roomId: { $in: [10000484, 10000523, 10001044, 10001113] }\ }) ``` #### 方法 3:使用模板字符串(仅限支持 ES6 的环境) 用反引号 `` ` `` 包裹代码,支持直接换行: ```javascript const query = `db.liveWatchRecordDat.find({ roomId: { $in: [10000484, 10000523, 10001044, 10001113] } })`; ``` --- ### 补充说明 1. **MongoDB Shell 的交互模式** 如果直接在 MongoDB Shell 中输入多行代码,可以忽略此错误(Shell 会自动等待闭合的 `}` 输入完成),但需确保括号匹配。 2. **常见触发场景** 该错误通常出现在将代码作为字符串传递给某些工具时(如脚本执行器、自动化工具),此时需按上述方法处理换行符。 --- ### 最终建议 根据你的使用场景选择解决方案。若在 MongoDB Shell 中直接运行,保持原格式即可;若在脚本或字符串中执行,推荐使用 **方法 1** 或 **方法 2**。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值