我知道>=运算符意味着大于或等于,但我在一些源代码中看到过=>。这个运算符是什么意思?
代码如下:
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));
正如其他人所说,这是一种创建函数的新语法。
但这类函数与普通函数不同:
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
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));