我知道>=运算符意味着大于或等于,但我在一些源代码中看到过=>。这个运算符是什么意思?

代码如下:

promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => {
    if (!aDialogAccepted)
        return;

    saveAsType = fpParams.saveAsType;
    file = fpParams.file;

    continueSave();
}).then(null, Components.utils.reportError);

当前回答

再举一个例子,说明lambda不使用map也能做什么:

a = 10
b = 2

var mixed = (a,b) => a * b; 
// OR
var mixed = (a,b) => { (any logic); return a * b };

console.log(mixed(a,b)) 
// 20

其他回答

再举一个例子,说明lambda不使用map也能做什么:

a = 10
b = 2

var mixed = (a,b) => a * b; 
// OR
var mixed = (a,b) => { (any logic); return a * b };

console.log(mixed(a,b)) 
// 20

这些是箭头函数

也称为胖箭头函数。它们是一种简洁明了的方法来编写函数表达式,例如function(){}。

箭头函数可以在定义函数时去掉function、return和{}。它们是一行程序,类似于Java或Python中的Lambda表达式。

不带参数的示例

const queue = ['Dave', 'Sarah', 'Sharon']; const nextCustomer = () => queue[0] console.log (nextCustomer ());/ /“大卫”

如果需要在同一个箭头函数中执行多条语句,那么在本例中,您需要将队列[0]封装在尖括号{}中。在这种情况下,return语句不能省略。

带有1个参数的示例

const queue = ['Dave', 'Sarah', 'Sharon']; const addCustomer = name => { queue.push(名称); }; addCustomer(“托比”); console.log(队列);// ['Dave', 'Sarah', 'Sharon', 'Toby']

你可以从上面省略{}。

当只有一个参数时,参数周围的括号()可以省略。

带有多个参数的示例

const addNumbers = (x, y) => x + y console.log (addNumbers(1、5));/ / 6

一个有用的例子

const fruits = [
    { name: 'Apple', price: 2 },
    { name: 'Bananna', price: 3 },
    { name: 'Pear', price: 1 }
];

如果我们想在一个数组中获取每个水果的价格,在ES5中我们可以这样做:

fruits.map(function(fruit) {
    return fruit.price;
}); // [2, 3, 1]

在ES6的新箭头函数中,我们可以让它更简洁:

fruits.map(fruit => fruit.price); // [2, 3, 1]

关于箭头函数的更多信息可以在这里找到。

这就是ECMAScript 6中引入的“箭头函数表达式”。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions

出于历史原因(如果wiki页面稍后更改),它是:

与函数表达式相比,箭头函数表达式具有更短的语法,并且在词法上绑定this值。箭头函数总是匿名的。

JavaScript箭头函数大致相当于python中的lambda函数或Ruby中的块。这些是具有自己特殊语法的匿名函数,并在其封闭作用域的上下文中操作。这意味着它们没有自己的“This”,而是从直接的封闭函数中访问one。

来自ECMA标准:

ArrowFunction不为参数定义本地绑定, 超级,这个,或者新的目标。任何对参数、super、this或new的引用。ArrowFunction中的target必须解析为 在词汇封闭的环境中绑定。通常这将是 直接封闭函数的函数环境。

通常你会读成“箭头函数表达式是传统函数表达式的紧凑替代”,这是不正确的。箭头函数不是传统函数的简写,它们的行为与传统函数不同。

语法

// Traditional Function
// Create their own scope inside the function
function (a){
  return a + 100;
}

// Arrow Function 
// Do NOT create their own scope
// (Each step along the way is a valid "arrow function")

// 1. Remove the word "function" and place arrow between the argument and opening body bracket
(a) => {
  return a + 100;
}

// 2. Remove the body braces and word "return" -- the return is implied.
(a) => a + 100;

// 3. Remove the argument parentheses (only valid with exactly one argument)
a => a + 100;

ES6 Arrow功能:

在javascript中=>是箭头函数表达式的符号。箭头函数表达式没有自己的this绑定,因此不能用作构造函数。例如:

Var单词= 'hi从外部对象'; 让obj = { Words: 'hi from inside object', Talk1:() => {console.log(this.words)}, Talk2: function () {console.log(this.words)} } obj.talk1 ();//没有它自己的this绑定,this ===窗口 obj.talk2 ();//有它自己的this绑定,这是obj

箭头函数的使用规则:

如果只有一个参数,可以省略参数的括号。 如果你在同一行返回一个表达式,你可以省略{}和return语句

例如:

val * 2; //它在同一行,返回一个表达式,因此省略{},表达式隐式返回 //也只有一个参数,因此参数周围的括号被省略 console.log (times2 (3));