Example of a circular reference in Javascript?

2019-01-13 17:18发布

I was wondering if anyone has a good, working example of a circular reference in javascript? I know this is incredibly easy to do with closures, but have had a hard time wrapping my brain around this. An example that I can dissect in Firebug would be most appreciated.

Thanks

8条回答
祖国的老花朵
2楼-- · 2019-01-13 18:15
var b = [];
var a = [];
a[0] = b;
b[0] = a;

Printing a or b would return Circular.

查看更多
Root(大扎)
3楼-- · 2019-01-13 18:18
function circular(arg){
    var count = 0;

    function next(arg){
        count++;
        if(count > 10) return;
        if(arg){
            console.log('hava arg: ' + arg);
            next();
        }else{
            console.log('no arg');
            next('add');
        }
    }
    next();
}
circular();

Circular and with a closures.

查看更多
登录 后发表回答