var some = [] ['forEach'] ['constructor'];

时间:2017-03-24 21:04:49

标签: javascript arrays

我无法理解这段javascript代码,请任何人帮助我理解这一点。

var some = []['forEach']['constructor'];

2 个答案:

答案 0 :(得分:1)

[].forEach.constructor相同,与Array.prototype.forEach.constructor基本相同。

答案 1 :(得分:0)

以下是如何分解它:

[]['forEach']['constructor'];

// Convert bracket property access notation to dot notation
[].forEach.constructor;

// `forEach` is a property of Array.prototype
Array.prototype.forEach.constructor;

// `forEach` is a function reference with a 
// `constructor` property, due to `Function.prototype`
Function.prototype.constructor;

// `Constructor.prototype.constructor === Constructor`, generally
Function;


console.log(
  []['forEach']['constructor'] === Function //=> true
);

如果您问为什么这段代码会有用,我不太确定。可能是混淆。

相关问题