我在一些示例代码中遇到了这个,我完全迷失了.
const addCounter = (list) => {
return [...list, 0]; // This is the bit I am lost on, and I don't know about [...list, 0]
}
Run Code Online (Sandbox Code Playgroud)
显然以上等于以下内容:
const addCounter = (list) => {
return list.concat([0]);
}
Run Code Online (Sandbox Code Playgroud)
任何建议或解释都非常感谢.
我见过
const num = 123456789000000000000n;
并且不知道数字文字末尾的 n 是做什么的?
在撰写本文时,在网上搜索“JavaScript 中数字文字后面的字符 'n' 是什么意思”时,没有任何结果。
x <<= y (x = x << y)x >>= y (x = x >> y)x >>>= y (x = x >>> y)x &= y (x = x & y)x ^= y (x = x ^ y)x |= y (x = x | y)这些不同的运营商做了什么?
我正在建立一个应用程序,我在其中一个锅炉板项目中找到了一条线.
(state = {}) => state
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释上述行的含义是什么?这是javascript ES6标准.
我不知道代码是如何const countFrom = x => () => (x++, x);工作的:
const countFrom = x => () => (x++, x);
let a = countFrom(1)
console.log('output:', a()) // output: 2
console.log('output:', a()) // output: 3
console.log('output:', a()) // output: 4
console.log('output:', a()) // output: 5Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {min-height: 100%!important; top: 0;}Run Code Online (Sandbox Code Playgroud)
有人能告诉"Angular简介"示例中的下面代码中的"..."是什么吗?
getHeroes() {
this.backend.getAll(Hero).then( (heroes: Hero[]) => {
this.logger.log(`Fetched ${heroes.length} heroes.`);
this.heroes.push(...heroes); // fill cache
});
Run Code Online (Sandbox Code Playgroud) 我最近正在阅读有关Javascript的内容,并遇到了一些对我来说似乎陌生的语法:
const max = {a: 1, b: 2, c: 3}
|> Object.values
|> (_ => Math.max(..._))
Run Code Online (Sandbox Code Playgroud)
|>在这种情况下到底意味着什么?
我有一个字符串aman/gupta,我想替换它aman$$gupta,为此我使用JavaScript replace方法如下:
let a = "aman/gupta"
a = a.replace("/", "$")
console.log(a) // 'aman$gupta'
a = "aman/gupta"
a = a.replace("/", "$$")
console.log(a) // 'aman$gupta'
a = "aman/gupta"
a = a.replace("/", "$$$")
console.log(a) // 'aman$$gupta'Run Code Online (Sandbox Code Playgroud)
为什么第一和第二种情况相同,当我使用$$$而不是$$?时,我得到了预期的结果?
我试图了解编写ES6的一些简写方法.我在下面的例子中无法完全理解的是最后一个简写"({length})" - 我理解它确实有效,并且它获取了数组的长度属性,但不是为什么.如何在另一个场景中应用此语法,而不涉及数组?
//Declare array
var materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
//Long version - ok
materials.map(function(material) {
return material.length;
});
//Arrow function - ok
materials.map((material) => {
return material.length;
});
//Shorthand arrow function - ok
materials.map(str => str.length);
//What? :)
materials.map(({length}) => length));
Run Code Online (Sandbox Code Playgroud)
上面的例子来自箭头函数的mozilla文档. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
javascript ×10
ecmascript-6 ×3
arrays ×1
bigint ×1
literals ×1
operators ×1
string ×1
syntax ×1