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

代码如下:

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

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

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

当前回答

使用Arrowfunction添加简单的CRUD示例

 //Arrow Function
 var customers   = [
   {
     name: 'Dave',
     contact:'9192631770'
   },
   {
     name: 'Sarah',
     contact:'9192631770'
   },
   {
     name: 'Akhil',
     contact:'9928462656' 
   }],

// No Param READ
 getFirstCustomer = () => { 
   console.log(this);
   return customers[0];
 };
  console.log("First Customer "+JSON.stringify(getFirstCustomer())); // 'Dave' 

   //1 Param SEARCH
  getNthCustomer = index=>{
    if( index>customers.length)
    {
     return  "No such thing";
   }
   else{
       return customers[index];
     } 
  };
  console.log("Nth Customer is " +JSON.stringify(getNthCustomer(1))); 

   //2params ADD
  addCustomer = (name, contact)=> customers.push({
     'name': name,
     'contact':contact
    });
  addCustomer('Hitesh','8888813275');
  console.log("Added Customer "+JSON.stringify(customers)); 

  //2 param UPDATE
  updateCustomerName = (index, newName)=>{customers[index].name= newName};
  updateCustomerName(customers.length-1,"HiteshSahu");
  console.log("Updated Customer "+JSON.stringify(customers));

  //1 param DELETE
  removeCustomer = (customerToRemove) => customers.pop(customerToRemove);
  removeCustomer(getFirstCustomer());
  console.log("Removed Customer "+JSON.stringify(customers)); 

其他回答

正如所有其他答案已经说过的,它是ES2015箭头函数语法的一部分。更具体地说,它不是一个操作符,而是一个标点符号,将参数与正文分隔开来。例如(params) => {/* body */}。

正如其他人所说,这是一种创建函数的新语法。

但这类函数与普通函数不同:

They bind the this value. As explained by the spec, An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immediately enclosing function. Even though an ArrowFunction may contain references to super, the function object created in step 4 is not made into a method by performing MakeMethod. An ArrowFunction that references super is always contained within a non-ArrowFunction and the necessary state to implement super is accessible via the scope that is captured by the function object of the ArrowFunction. They are non-constructors. That means they have no [[Construct]] internal method, and thus can't be instantiated, e.g. var f = a => a; f(123); // 123 new f(); // TypeError: f is not a constructor

我读过,这是ES6中箭头函数的符号

var a2 = a.map(function(s){ return s.length });

使用箭头函数可以写成

var a3 = a.map( s => s.length );

MDN文档

这些是箭头函数

也称为胖箭头函数。它们是一种简洁明了的方法来编写函数表达式,例如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]

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

再举一个例子,说明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