Javascript 中循环引用的示例?

发布于 2024-08-05 22:55:09 字数 111 浏览 3 评论 0原文

我想知道是否有人有一个很好的、有效的 javascript 循环引用示例?我知道用闭包来做到这一点非常容易,但我很难理解这一点。如果我能在 Firebug 中剖析一个例子,我将非常感激。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

左秋 2024-08-12 22:55:09

创建循环引用的一个简单方法是让一个对象在属性中引用自身:

function Foo() {
  this.abc = "Hello";
  this.circular = this;
}

var foo = new Foo();
alert(foo.circular.circular.circular.circular.circular.abc);

这里 foo 对象包含对其自身的引用。

对于闭包,这通常更加隐式,仅在作用域中具有循环引用,而不是作为某个对象的显式属性:

var circular;

circular = function(arg) {
  if (arg) {
    alert(arg);
  }
  else {
    // refers to the |circular| variable, and by that to itself.
    circular("No argument");
  }
}

circular("hello");
circular();

这里保存在circular中的函数引用circular变量,从而对其自身而言。它隐式地持有对自身的引用,从而创建了循环引用。即使circular现在超出了范围,它仍然是从函数范围中引用的。简单的垃圾收集器不会识别此循环,也不会收集该函数。

A simple way to create a circular reference is to have an object that refers to itself in a property:

function Foo() {
  this.abc = "Hello";
  this.circular = this;
}

var foo = new Foo();
alert(foo.circular.circular.circular.circular.circular.abc);

Here the foo object contains a reference to itself.

With closures this is usually more implicit, by just having the circular reference in scope, not as an explicit property of some object:

var circular;

circular = function(arg) {
  if (arg) {
    alert(arg);
  }
  else {
    // refers to the |circular| variable, and by that to itself.
    circular("No argument");
  }
}

circular("hello");
circular();

Here the function saved in circular refers to the circular variable, and thereby to itself. It implicitly holds a reference to itself, creating a circular reference. Even if circular now goes out of scope, it is still referenced from the functions scope. Simple garbage collectors won't recognize this loop and won't collect the function.

身边 2024-08-12 22:55:09

或者更简单,一个“包含”自身的数组。参见示例:

var arr = [];
arr[0] = arr;

Or even simpler, an array "containing" itself. See example:

var arr = [];
arr[0] = arr;
你丑哭了我 2024-08-12 22:55:09

可能是定义循环对象的最短方法。

a = {}; a.a = a;

Probably the shortest way to define a cyclic object.

a = {}; a.a = a;
枫林﹌晚霞¤ 2024-08-12 22:55:09
window.onload = function() {
  hookup(document.getElementById('menu'));

  function hookup(elem) {
    elem.attachEvent( "onmouseover", mouse);

    function mouse() {
    }
  }
}

正如您所看到的,处理程序嵌套在附加程序中,这意味着它在调用方的范围内是封闭的。

window.onload = function() {
  hookup(document.getElementById('menu'));

  function hookup(elem) {
    elem.attachEvent( "onmouseover", mouse);

    function mouse() {
    }
  }
}

As you can see, the handler is nested within the attacher, which means it is closed over the scope of the caller.

秋叶绚丽 2024-08-12 22:55:09

或者使用 ES6:

class Circular {
  constructor() {
    this.value = "Hello World";
    this.self = this;
  }
}

circular = new Circular();

Or using ES6:

class Circular {
  constructor() {
    this.value = "Hello World";
    this.self = this;
  }
}

circular = new Circular();
固执像三岁 2024-08-12 22:55:09

你可以这样做:

  • window.window...window
  • var Circle = {};圆.圆 = 圆;
  • var 圆 = [];圆[0] = 圆;或者circle.push(circle)
  • function Circle(){this.self = this}; var 圆 = new Circle()

You can do:

  • window.window...window
  • var circle = {}; circle.circle = circle;
  • var circle = []; circle[0] = circle; or circle.push(circle)
  • function Circle(){this.self = this}; var circle = new Circle()
梦断已成空 2024-08-12 22:55:09
var b = [];
var a = [];
a[0] = b;
b[0] = a;

打印 ab 将返回 Circular

var b = [];
var a = [];
a[0] = b;
b[0] = a;

Printing a or b would return Circular.

梦断已成空 2024-08-12 22:55:09
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();

圆形并带有封闭件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文