所以我可以通过这个简洁的辅助函数并且对语法感到困惑。有一个变量(bool声明为true,看起来是一个数组。它使用括号对象表示法,但是然后要比较bool[j]或[i]是否为真,没有添加[i]或[j] 到对象映射。
const helper = (word, words) => {
let bool = [true];
//if you console.log(typeof bool) returns object ??
//This comes out as an Obj at first glance I thought it was an arr, but its bracket notation
for (var i = 1; i <= word.length; i++) {
for (var j = 0; j <= i; j++) {
//how is bool[j] being evaluated? or i if its an obj?
if (bool[j] === true && words[word.substring(j, i)] === true) {
bool[i] = true;
break;
} else {
bool[i] = false;
}
}
}
return console.log(bool[word.length] ? true : false);
}
helper('aa', ['aa', 'aabb', 'someotherword']);
最佳答案
在 JavaScript 中,数组是 Object 的一个实例,它们的类型将如此注册。
像这样的语句
foo[i] = 'bar'
将在数组(或对象)foo
的第 i
个从零开始的索引处分配值“bar”。这发生在您发布的代码中的 for
循环中。
var a = []
console.log(a instanceof Object)
a[3] = 4
console.log(a)
关于javascript - 了解 javascript 高级语法和对象表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083891/