【问题标题】:understanding javascript advanced syntax, and object notation了解 javascript 高级语法和对象表示法
【发布时间】:2019-03-10 02:24:48
【问题描述】:

所以我可以通过这个简洁的辅助函数并且对语法感到困惑。有一个变量 (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 arrays algorithm oop boolean


    【解决方案1】:

    在 JavaScript 中,数组是 Object 的一个实例,它们的类型将被注册为对象。

    类似的声明

    foo[i] = 'bar'
    

    将在数组(或对象)foo 的从零开始的索引 i 处分配值“bar”。这发生在您发布的代码中的 for 循环中。

    var a = []
    
    console.log(a instanceof Object)
    
    a[3] = 4
    
    console.log(a)

    【讨论】:

    • 啊蚱蜢,谢谢你部分清除了它。我厌倦了给你一个代表点,但它不允许是因为首先需要 15 个帖子
    • @JoshuaAguilar 如果您愿意,您可以接受答案(通过单击答案投票附近的复选标记)或要求更多说明。
    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多