Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关于ES6中使用return时的分号_Javascript_Ecmascript 6_Arrow Functions - Fatal编程技术网

Javascript 关于ES6中使用return时的分号

Javascript 关于ES6中使用return时的分号,javascript,ecmascript-6,arrow-functions,Javascript,Ecmascript 6,Arrow Functions,我是JS的新手。如果我添加这样的分号,${this.name}就是${el};在参数列表之后,我将得到一个错误“uncaughtsyntaxerror:missing”。我可以知道为什么吗?因为在ES5中,我可以使用像returnthis这样的分号。name+'是'+el'的朋友; 非常感谢你 function Person(name) { this.name = name; } ES6 Person.prototype.myFriends5 = function(friends) {

我是JS的新手。如果我添加这样的分号,${this.name}就是${el};在参数列表之后,我将得到一个错误“uncaughtsyntaxerror:missing”。我可以知道为什么吗?因为在ES5中,我可以使用像returnthis这样的分号。name+'是'+el'的朋友; 非常感谢你

function Person(name) {
    this.name = name;
}

ES6
Person.prototype.myFriends5 = function(friends) {
var arr = friends.map((el) =>
     `${this.name} is friends with ${el}`
);
console.log(arr);
}
var friends = ['Bob', 'Jane', 'Mark'];
new Person('John').myFriends5(friends);

箭头函数可以用两种方式编写:

(params) => expression

其中
body
与传统函数(一系列语句)的主体类似

使用第一种格式时,不能使用

console.log(a;)
a = (`${this.name} is friends with ${el}`;)
(params) => `${this.name} is friends with ${el}`;
第一种形式为以下简称:

(params) => {
    return expression;
}
什么是有效表达式的经验法则是,它可以放在括号内。因此,如果你能写下如下内容:

a = (something)
然后你可以写:

(params) => something
既然你不会写:

console.log(a;)
a = (`${this.name} is friends with ${el}`;)
(params) => `${this.name} is friends with ${el}`;
你不能写:

console.log(a;)
a = (`${this.name} is friends with ${el}`;)
(params) => `${this.name} is friends with ${el}`;

啊,我可以知道在(params)=>表达式中,这个表达式总是表示“返回某物”吗?如果我写了(params)=>leti=10,这是否意味着:(params)=>{returnleti=10;}?非常感谢你!是的,总是这样。既然
let
是一个语句,而不是一个表达式,那就行不通了。基本上,如果你不会写
(废话)
你就不会写
=>废话了,非常感谢!所以我可以写(params)=>I=10和(params)=>{returni=10;}对吗?它会先给i赋值10,然后返回i,也就是10?是的,你可以这样做。它将分配给外部范围中声明的变量。