JS中的这种语言构造是什么?

时间:2018-09-17 23:05:05

标签: javascript

能否请您帮助我了解Java语言中的这是什么?什么是 a ,什么是 a ['b'] ?如何在iframe和父窗口中访问构造的声明内容?

var a = window['parent'];
if(a) {
    if(a['b']){
        a['b']({
            "c": 1,
            "d": {
                "e": "f",
                "g": false,
                "h": 1
            },
            "i": 0,
            "j": true
        });
    }
}

如果可能,请给我一个链接,我可以在其中阅读有关此构造的信息。

非常感谢。

1 个答案:

答案 0 :(得分:0)

好吧,无论如何,这里没有很好的问题,看来您真的很想弄清楚,所以我会回答:

var a = window['parent']; // here we access the "parent" property of window. Could as well be accessed as "window.parent" (same results)
if(a) { // here we check if "a" is truthy [1]
    if(a['b']){ // here we assume that "a" is an object [2] (associative array in some languages), because we are accessing its "b" property. Also we are checking if the "b" property is truthy. 
        a['b']({ // here we assume that a['b'] is a function, because  parentheses are used. We then pass an object (denoted by curly braces) as an argument.
            "c": 1, // this is object's property (as well as others below)
            "d": { // the nested (child) object begins
                "e": "f",
                "g": false,
                "h": 1
            },
            "i": 0,
            "j": true
        });
    }
}

[1]真实-https://developer.mozilla.org/en-US/docs/Glossary/Truthy

[2]个对象-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

P.S。通常,这是错误的代码。您永远不要尝试以这种方式编写代码。事情可能会出错,例如a ['b']可能不是函数,而是其他某些真实类型。同样,整个事情显然也不可读。