Skip to main content

Access parent"s parent from javascript object



Somthing like







var life= {

users : {

guys : function(){ this.SOMTHING.mameAndDestroy(this.girls); },

girls : function(){ this.SOMTHING.kiss(this.boys); },

},

mameAndDestroy : function(group){ },

kiss : function(group){ }

};







this.SOMTHING is what I imagine the format is, but it might not be. What will step back up to the parent of an object?



Source: Tips4all

Comments

  1. In this case, you could use life to reference the parent object. Or you could store a reference to life in the users object. There can't be a fixed parent available to you in the language, because users is just a reference to an object, and there could be other references...

    var death = { residents : life.users };
    life.users.smallFurryCreaturesFromAlphaCentauri = { exist : function() {} };
    // death.residents.smallFurryCreaturesFromAlphaCentauri now exists
    // - because life.users references the same object as death.residents!


    You might find it helpful to use something like this:

    function addChild(ob, childName, childOb)
    {
    ob[childName] = childOb;
    childOb.parent = ob;
    }

    var life= {
    mameAndDestroy : function(group){ },
    kiss : function(group){ }
    };

    addChild(life, 'users', {
    guys : function(){ this.parent.mameAndDestroy(this.girls); },
    girls : function(){ this.parent.kiss(this.boys); },
    });

    // life.users.parent now exists and points to life

    ReplyDelete
  2. If I'm reading your question correctly, objects in general are agnostic about where they are contained. They don't know who their parents are. To find that information, you have to parse the parent data structure. The DOM has ways of doing this for us when you're talking about element objects in a document, but it looks like you're talking about vanilla objects.

    ReplyDelete
  3. Here you go:

    var life={
    users:{
    guys:function(){ life.mameAndDestroy(life.users.girls); },
    girls:function(){ life.kiss(life.users.guys); }
    },
    mameAndDestroy : function(group){
    alert("mameAndDestroy");
    group();
    },
    kiss : function(group){
    alert("kiss");
    //could call group() here, but would result in infinite loop
    }
    };

    life.users.guys();
    life.users.girls();


    Also, make sure you don't have a comma after the "girls" definition. This will cause the script to crash in IE (any time you have a comma after the last item in an array in IE it dies).

    See it run

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.