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

代码如下:

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

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

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

当前回答

用符号(=>)表示的箭头函数可以帮助您创建匿名函数和方法。这将导致更短的语法。例如,下面是一个简单的“Add”函数,它返回两个数字的加法。

function Add(num1 , num2 ){
return num1 + num2;
}

通过使用如下所示的“箭头”语法,上面的函数变得更短。

上述代码有两部分,如上图所示:-

输入:—指定匿名函数的输入参数。

逻辑:-这部分出现在符号“=>”之后。本节符合实际函数的逻辑。

许多开发人员认为箭头函数使您的语法更短,更简单,从而使您的代码可读。

如果你相信上面这句话,那么让我向你保证这是一个神话。如果你想一下,一个正确编写的带有名称的函数比在一行中使用箭头符号创建的神秘函数更具可读性。

箭头函数的主要用途是确保代码运行在 呼叫者上下文。

请参阅下面的代码,其中定义了一个全局变量“context”,该全局变量在函数“SomeOtherMethod”中访问,该函数从其他方法“SomeMethod”中调用。

这个“SomeMethod”具有本地“context”变量。现在因为“SomeOtherMethod”是从“SomeMethod”中调用的,我们期望它显示“本地上下文”,但它显示“全局上下文”。

var context = “global context”;

function SomeOtherMethod(){
alert(this.context);
}

function SomeMethod(){
this.context = “local context”;
SomeOtherMethod();
}

var instance = new SomeMethod();

但是如果使用Arrow函数替换调用,它将显示“本地上下文”。

var context = "global context";
   
    function SomeMethod(){
        this.context = "local context";
        SomeOtherMethod = () => {
            alert(this.context);
        }
        SomeOtherMethod();
    }
    var instance = new SomeMethod();

我鼓励你阅读这个链接(JavaScript中的箭头函数),它解释了JavaScript上下文的所有场景,以及在哪些场景中调用者上下文不受尊重。

你也可以在我制作的youtube视频中看到用javascript演示的箭头函数,它实际上演示了上下文这个术语。

其他回答

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

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

使用箭头函数可以写成

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

MDN文档

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

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

用符号(=>)表示的箭头函数可以帮助您创建匿名函数和方法。这将导致更短的语法。例如,下面是一个简单的“Add”函数,它返回两个数字的加法。

function Add(num1 , num2 ){
return num1 + num2;
}

通过使用如下所示的“箭头”语法,上面的函数变得更短。

上述代码有两部分,如上图所示:-

输入:—指定匿名函数的输入参数。

逻辑:-这部分出现在符号“=>”之后。本节符合实际函数的逻辑。

许多开发人员认为箭头函数使您的语法更短,更简单,从而使您的代码可读。

如果你相信上面这句话,那么让我向你保证这是一个神话。如果你想一下,一个正确编写的带有名称的函数比在一行中使用箭头符号创建的神秘函数更具可读性。

箭头函数的主要用途是确保代码运行在 呼叫者上下文。

请参阅下面的代码,其中定义了一个全局变量“context”,该全局变量在函数“SomeOtherMethod”中访问,该函数从其他方法“SomeMethod”中调用。

这个“SomeMethod”具有本地“context”变量。现在因为“SomeOtherMethod”是从“SomeMethod”中调用的,我们期望它显示“本地上下文”,但它显示“全局上下文”。

var context = “global context”;

function SomeOtherMethod(){
alert(this.context);
}

function SomeMethod(){
this.context = “local context”;
SomeOtherMethod();
}

var instance = new SomeMethod();

但是如果使用Arrow函数替换调用,它将显示“本地上下文”。

var context = "global context";
   
    function SomeMethod(){
        this.context = "local context";
        SomeOtherMethod = () => {
            alert(this.context);
        }
        SomeOtherMethod();
    }
    var instance = new SomeMethod();

我鼓励你阅读这个链接(JavaScript中的箭头函数),它解释了JavaScript上下文的所有场景,以及在哪些场景中调用者上下文不受尊重。

你也可以在我制作的youtube视频中看到用javascript演示的箭头函数,它实际上演示了上下文这个术语。

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

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

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