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

代码如下:

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

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

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

当前回答

正如其他人所述,常规(传统)函数使用调用该函数的对象的this(例如,被单击的按钮)。相反,箭头函数使用定义该函数的对象中的this。

考虑两个几乎相同的函数:

regular = function() {
  ' Identical Part Here;
}


arrow = () => {
  ' Identical Part Here;
}

下面的代码段演示了这对于每个函数所代表的内容之间的基本区别。常规函数输出[object HTMLButtonElement],而箭头函数输出[object Window]。

<html> <button id="btn1">Regular: `this` comes from "this button"</button> <br><br> <button id="btn2">Arrow: `this` comes from object that defines the function</button> <p id="res"/> <script> regular = function() { document.getElementById("res").innerHTML = this; } arrow = () => { document.getElementById("res").innerHTML = this; } document.getElementById("btn1").addEventListener("click", regular); document.getElementById("btn2").addEventListener("click", arrow); </script> </html>

其他回答

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

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

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

正如其他人所述,常规(传统)函数使用调用该函数的对象的this(例如,被单击的按钮)。相反,箭头函数使用定义该函数的对象中的this。

考虑两个几乎相同的函数:

regular = function() {
  ' Identical Part Here;
}


arrow = () => {
  ' Identical Part Here;
}

下面的代码段演示了这对于每个函数所代表的内容之间的基本区别。常规函数输出[object HTMLButtonElement],而箭头函数输出[object Window]。

<html> <button id="btn1">Regular: `this` comes from "this button"</button> <br><br> <button id="btn2">Arrow: `this` comes from object that defines the function</button> <p id="res"/> <script> regular = function() { document.getElementById("res").innerHTML = this; } arrow = () => { document.getElementById("res").innerHTML = this; } document.getElementById("btn1").addEventListener("click", regular); document.getElementById("btn2").addEventListener("click", arrow); </script> </html>

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

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

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

使用箭头函数可以写成

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

MDN文档

使用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));