2015-12-28 48 views
0

下面的代碼:不能理解這個匿名函數的語法在Javascript

angular.module('socially').controller('PartiesListCtrl', function ($scope) 
    { 
    $scope.helpers({ 
      parties:() => { 
      return Parties.find({}); 
      } 
    }); 
    }); 

演示在Angular Meteor Tutorial

不能理解用於parties:對象的語法。爲什麼使用=>?這種匿名函數有更多解釋嗎?

+1

我可能是錯的,但這看起來像一個箭頭函數。 – Rajesh

+0

感謝新的箭頭功能語法 –

回答

4

這是一個箭頭函數,它是今年被接受的ES2015標準的新語法。不僅箭頭功能在聲明較短,有時看起來更好,他們也分享他們周圍的代碼

!function() { 
    this.name = 'global'; 

    var nonArrowFunc = function() { 
    console.log(this.name); // undefined, because this is bind to nonArrowFunc 
    } 

    var arrowFunc =() => { 
    console.log(this.name); // this taken from outer scope 
    } 


}(); 

結合上下文,你可以閱讀更多關於箭頭功能hereherehere

+0

是否與最新的智能手機和科爾多瓦html查看器兼容 –

+0

@aj_blk不確定,但是你總是可以使用babel編譯器來將你的新的驚人的ES2015 JS代碼轉換成舊的好的ES5 JS –