Example of a circular reference in Javascript?

前端 未结 8 793
说谎
说谎 2020-11-27 16:36

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 wr

相关标签:
8条回答
  • 2020-11-27 17:40
    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.

    0 讨论(0)
  • 2020-11-27 17:42

    Or using ES6:

    class Circular {
      constructor() {
        this.value = "Hello World";
        this.self = this;
      }
    }
    
    circular = new Circular();
    
    0 讨论(0)
提交回复
热议问题