javascript对象如何使用'this'? [重复]

问题描述 投票:-1回答:1

此代码有效,但是如果我将N.max更改为this.max,它将停止工作。我不知道为什么它不起作用

const N = {
  min: function(arr) {
    arr.sort((a, b) => a - b);
    return arr.reduce(function(a, b) {
      return a * b / N.max(a, b); // here
    });
  },
  max: (a, b) => (b) ? N.max(b, a % b) : a // and here
}

let arr = [2, 6, 8, 14];

console.log(N.min(arr));
javascript object this
1个回答
-1
投票

这意味着'当前对象',即使您处于'N',也不会引用该const。”>

[如果您想查看使用'this'实际指的是什么,那么它是JavaScript,我建议您添加console.log(this),这样您就可以看到它指的是什么。

© www.soinside.com 2019 - 2024. All rights reserved.