SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here is how I intend to use the this keyword.

    Code JavaScript:
    Dom = {
        get: function (el) {
        },
        ...
        attr: {
            get: function (el, param) {
                // use Dom.get, hopefully without Dom and instead with the this keyword
            },
            set: function (el, objPair) {
                // use Dom.get, hopefully without Dom and instead with the this keyword
            }
        }
    };

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    humbug = foo;
    This line will throw an error when it is in an object literal.

    Forget foobar. Use objects that have meaning, and methods that have some effect. And test your code.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Yes, thankyou. The example has been corrected.

    The whole purpose of using foobar is to abstract the question so that we can focus not on the names of the objects, but on the question being asked instead.

    If the this keyword refers to foo.baz, is it possible to obtain from that a reference to foo?
    Last edited by paul_wilkins; Jan 6, 2008 at 22:48.

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    If the this keyword refers to foo.baz, is it possible to obtain from that a reference to foo?
    Yes, the this keyword is the reference to foo.

    One thing about having getters and setters in JavaScript, the user can easily set the variable without calling your setter method. This due to JavaScript's dynamic nature.

    Lets say that you wanna check that a Cat's weight is not negative (invalid):
    Code:
    var Cat = {
    	weight: null,
    	setWeight: function(weight) {
    		if (weight > 0) {
    			this.weight = weight;
    		}	
    	},
    	getWeight: function() {
    		return this.weight;
    	}
    };
    
    // Set weight to 3
    Cat.setWeight(3);
    It's not really possible to stop anybody from ignoring your setter and doing this:
    Code:
    Cat.weight = -1000;
    Safari, Opera 9.5 and Firefox support real setters and getters. Check it out.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    It's not really possible to stop anybody from ignoring your setter
    I beg to differ. There are private static members in javascript.

    Code JavaScript:
    var Cat = (function () {
        var weight = null;
        function setWeight(value) {
            weight = value;
        }
        function getWeight() {
            return weight;
        }
        function constructor() {
            var weight = getWeight();
        }
        constructor.setWeight = function (value) {
            if (value > 0) {
                setWeight(value);
            }
        };
        constructor.getWeight = function () {
            return weight;
        };
        return constructor;
    })();
     
    Cat.setWeight(4);
    // Cat.getWeight() is 4
    // Cat.weight is undefined

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I think that I'm going to have to make the parent explicit if it can't be otherwise determined.

    Code JavaScript:
    var attr = {
        parent: Dom,
        get: function (el, param) {
            el = this.parent.get(el); // in case an id is provided
            ...
        }
    };
    Dom.attr = attr;

    Now the Dom.attr.get function can use this.parent to access the Dom functions.

    Is this a sound design principle, or should they be explicitly called with Dom.get(el) instead of using this.parent.get(el)

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but we were talking about JSON syntax. You can have a private field using a constructor yes.


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
  •