let objNewWay = {
width:400,
height:300,
area: () => this.width*this.height
};
console.log(objNewWay.area()); // NaN
let objOldWay = {
width:400,
height:300,
area: function() {
return this.width*this.height;
}
};
console.log(objOldWay.area()); // 120000
Run Code Online (Sandbox Code Playgroud)
我不明白为什么 Javascript 对象中的箭头函数似乎不起作用。如果您查看上面的代码,第一个 console.log 将打印 NaN,第二个 console.log 将按预期打印数字。
我试图在类型脚本接口中编写箭头函数,如下所示,但出现错误“类‘ToastrService’错误地实现了接口‘IToastr’。”
interface IToastr{
(message:string,title:string):void;
}
@Injectable()
export class ToastrService implements IToastr {
(message:string,title:string):void {
toastr.success(message,title);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:我们可以在 TS 接口中定义一个返回类型为 void 的函数签名吗?
我也尝试搜索谷歌,但没有找到任何例子。谢谢 !!
var cat = {
lives: 9,
jumps: () => {
console.log(this.lives)
}
}
Run Code Online (Sandbox Code Playgroud)
我听说箭头函数中的“this”关键字将指向父作用域,所以我尝试在节点的全局对象中放置一个带有lives=9的变量,但这似乎不起作用,我是否遗漏了一些东西?
var lives=9
var cat = {
lives: 9,
jumps: () => {
console.log(this.lives)
}
}
Run Code Online (Sandbox Code Playgroud) 箭头函数返回函数文本 - "(n)=> 5 + n"而不是结果(6).我究竟做错了什么?
let n = 1;
let newText = (n) => 5 + n;
document.write(newText);
Run Code Online (Sandbox Code Playgroud) 我知道如何将箭头函数用于Array.filter()方法,因为您只需要一个值:var a = array.filter(val=>{return val<=5;});
但是如何像排序一样使用多个值呢?我试过了,但是没有用:array.sort(a,b)=>{return b-a;});
我一直在使用Javascript.最近他们在ES6中增加了箭头功能,这些功能也有好处
this但是它有一些使用箭头功能的缺点:
Function原型我想问下面的事情:
Function原型?我问这个是因为mdn页面说它没有原型,但没有解释为什么它没有原型.
我阅读了Eric Elliot的这个系列文章,我认为我们正在使用javascript中的函数式编程方法,并且由于计划发布,我们是否有机会在不久的将来删除OOPS?
任何版本的 Javascript 是否支持箭头函数中数组的解构?例如
const items = [ [1, 2], [3, 4] ];
const sums = items.map( [a, b] => a + b );
Run Code Online (Sandbox Code Playgroud) 我需要使用箭头函数设置数组属性之一。在我的示例代码中,我添加了随机键和箭头函数。但是当我控制台输出时,我看不到“随机”键。这可以使用箭头函数来完成。
let Allitems = {"items":[{"id":74489},{"id":64489},{"id":53489}]};
const newarr = Allitems.items.map(e => ({...e, name:"newnamme", random: (e) => { return e.id + "random"; } }));
console.log(newarr);Run Code Online (Sandbox Code Playgroud)
// expected output
{"items":[
{"id":74489,name:"newnamme", random : "74489random"},
{"id":64489,name:"newnamme", random : "64489random"},
{"id":53489,name:"newnamme", random : "53489random"}]
}
Run Code Online (Sandbox Code Playgroud) javascript arrays function arrow-functions object-destructuring
所以据我所知,此时此刻显然是错误的,
return arg => arg*2
Run Code Online (Sandbox Code Playgroud)
是相同的
return (arg)=>{arg*2}
Run Code Online (Sandbox Code Playgroud)
我一直认为箭头函数在语法上更简洁。
但是用像这样的闭包来做这件事是行不通的。
function addTwoDigits(firstDigit){
return (secondDigit)=>{firstDigit + secondDigit}
}
let closure = addTwoDigits(5);
console.log(closure(5)) // Undefined
Run Code Online (Sandbox Code Playgroud)
然而这很好
function addTwoDigitsV2(firstDigit){
return secondDigit => firstDigit + secondDigit
}
let closure2 = addTwoDigitsV2(10);
console.log(closure2(10))// 20
Run Code Online (Sandbox Code Playgroud) 例如:
count([2, 1, 4, 5, 2, 8], (e) => e === 2) // Expected: 2
count([1, 2, 3, 4], (e) => e > 1) // Expected: 3
Run Code Online (Sandbox Code Playgroud)
还需要使用forEach
我拥有的:
function count(arr, callback) {
let values = 0
arr.forEach(cb ? values++ : values + 0)
return values
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。有什么建议吗?
arrow-functions ×10
javascript ×9
ecmascript-6 ×2
angular ×1
arrays ×1
callback ×1
closures ×1
function ×1
node.js ×1
typescript ×1