简体   繁体   English

我不懂JavaScript代码段

[英]I don't understand a JavaScript code snippet

Hello I'm training making some assert suits libraries, but I have a big problem understanding the javascript behavior in a code section. 你好我正在训练制作一些断言套装库,但是我在解释代码部分中的javascript行为时遇到了很大的问题。 here the code. 这里的代码。

(function(){
    var results;
    this.assert = function(value, desc){
        var li = document.createElement('li');

        // I got two CSS classes "pass" and "fail"
        li.className = value ? 'pass' : 'fail';

        var txt = document.createTextNode(desc);

        li.appendChild(txt);
        results.appendChild(li);

        if(!value){
            li.parentNode.parentNode.className = 'fail';
        }
        return li;
    }
    this.test(name, fn){
        //I have an UL element with ID "results"
        results = document.getElementById('results');

        //but i will create another UL element
        var ul = document.createElement('ul');

        /*and here my trouble, I don't understand the sense of change
        the value of "results" variable, and why the reference to "results" ul element
        still serves, could you explain me that please?*/
        results = assert(true, name).appendChild(ul);
        fn();
    }
})();
//and here my library working
window.onload = function(){
    test('first test', function(){
        assert(2 + 2 == 4, 'True assertion');
    });
}

My big problem is understanding how does it work the change of variable "results". 我的一个大问题是理解变量“结果”的变化是如何起作用的。

From

//I have an UL element with ID "results"
results = document.getElementById('results');

To

results = assert(true, name).appendChild(ul);

I understand assert method creates a li element, but i don't understand why the reference to the results ul element still works. 我理解assert方法创建一个li元素,但我不明白为什么对结果 ul元素的引用仍然有效。 please explain me that. 请解释一下。

是否要追加断言返回的li结果?

results = ul.appendChild(assert(true,name));

The line following immediately invoked function expression (IIFE): 紧接着调用函数表达式(IIFE)后面的行:

(function(){
    var results;

creates a variable results within the function's execution context. 在函数的执行上下文中创建变量结果 This function: 这个功能:

   this.test(name, fn){
        //I have an UL element with ID "results"
        results = document.getElementById('results');

has that execution context on its scope chain. 在其作用域链上有该执行上下文。 The relationship remains after the outer function finishes, creating a closure. 在外部函数完成后,关系仍然存在,从而创建一个闭包。

Therefore, each time you call test , it is accessing the same results variable. 因此,每次调用test时 ,它都会访问相同的结果变量。

I suggest you read more about IIFEs and closures. 我建议你阅读更多关于IIFE和闭包的内容。

NB NB

You can't append a UL to a UL as they can only have LI as element child nodes. 您不能将UL附加到UL,因为它们只能将LI作为元素子节点。 Also, assuming that this within the IIFE is not sensible as in strict mode it will be undefined. 此外,假设IIFE中的这个在严格模式下是不合理的,它将是未定义的。 To fix that, pass the global object to the IIFE and use it: 要解决此问题,请将全局对象传递给IIFE并使用它:

(function (global) {
    var results;

    global.assert = function(value, desc){

 ...

}(this));

results was declared inside the function as var results and Javascript uses block function scope so inside the block quote function where it's given a reference to the new ul that's been created inside of the function itself. 结果在函数内部声明为var results ,Javascript使用 函数作用域,因此在 块引用 函数中,它给出了对函数本身内部创建的新ul的引用。 You should read more on closures and execution context. 您应该阅读有关闭包和执行上下文的更多信息。 This link should be of much help. 这个链接应该有很大帮助。

Closures 关闭

Each time the function is called, it creates a new execution context in which it is placed, the function is executed, the variable result is declared inside as private and then the same private variable is given a new reference inside the new block scope created and ul is appended to the results variable. 每次调用该函数时,它都会创建一个新的执行上下文,在其中放置它,执行该函数,将变量结果声明为私有,然后在创建的新块作用域内为相同的私有变量赋予新的引用, ul附加到结果变量。

When results = assert(true, name).appendChild(document.createElement('ul')); results = assert(true, name).appendChild(document.createElement('ul')); is called, the results variable in the this.test() scope is updated and since this.assert() is returning a li element, ul is appended to the results(which now is document.getElementById('results')) hence giving a new ul inside of the li created by this.assert() . 调用this.test()作用域中的结果变量,因为this.assert()返回一个li元素, ul将附加到results(which now is document.getElementById('results'))因此给出由this.assert()创建的li里面的新ul

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM