我知道>=运算符意味着大于或等于,但我在一些源代码中看到过=>。这个运算符是什么意思?
代码如下:
promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => {
if (!aDialogAccepted)
return;
saveAsType = fpParams.saveAsType;
file = fpParams.file;
continueSave();
}).then(null, Components.utils.reportError);
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));
使用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));