SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Threaded View

  1. #1
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)

    Can the parent of the this keyword be found in an object literal?

    Here is how I understand the this keyword to work when inside object literals.

    Code JavaScript:
    var foo = {
        bar: function () {
            // this = foo
        },
        baz: {
            bar: function () {
                 // this = foo.baz
        }
    };

    When I'm inside foo.baz.bar(), I want to call the foo.bar() function by using the this keyword.

    It can be done, but it takes a bit of a hack that I'm not pleased with.

    Code JavaScript:
    var foo = {
        bar: function () {
            // this = foo
        }
    };
    var bar = {
        humbug: foo,
        bar: function () {
            // this = bar and will soon be foo.baz
            // this.humbug.bar = foo.bar
        }
    };
    foo.baz = bar;

    So now foo.baz.bar can use the this keyword to call foo.bar

    My question is, is there a way to have foo.baz.bar call foo.bar without the humbug?
    Last edited by paul_wilkins; Jan 6, 2008 at 22:50.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •