Javascript recursive parent -> child -> parent -> child references, do I need to use an alternative? -
it's encountered on journey learning javascript , making application simultaneously.
var = function (parent) { this.parent = parent; }, b = new object(); b.a = new a(b);
this simple piece of code produces rather interesting.
console.log(b.a.parent.a.parent.a);
cause problems? because planning on using similar in application.
the reason use because need reference variable belonging parent.
it's same problem here. however, can't use closure here, since don't have outermost function!
a stripped down version of code follows:
var scratchpad = function (canvas) { var ctx = canvas.getcontext('2d'); ctx.childmustaccessthis = 3; ctx.brush = new brush(ctx); return ctx; }, brush = function (parent) { this.parent = parent; // other vars }; brush.prototype.getcontext = function () { return this.parent.childmustaccesthis; }
i need making multiple of these scratchpads (modified context objects) respective brushes.
i'm not sure make out of this, seems harmless got feeling it's bad practice.
i've tried other things, such closure described in linked post. of course, didn't work since scratchpad function not return instead returns modified ctx object, closure variables garbage collected (right? they're gone @ least).
the rest of tries relied on wrong understandings of how javascript objects behave, aren't worth mentioning.
so, should this? keep right now? or have suggestions better way of handling this?
that's called circular reference.
modern garbage collectors (as opposed netscape 2.0) handle fine.
note older versions of ie cannot handle circular references between dom objects , pure js objects.
Comments
Post a Comment