#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    569
    Rep Power
    70

    Any way to intercept object.property calls?


    Hi

    I'm not sure this is even possible, but I'm playing around with a specialized class and I would like to make it so undefined accesses are intercepted transparently to the user. For example, given object Foo which does not contain a "bar" property, I would like to have that access trigger some sort of "handleIfUndefined" method. It's easy enough to define a "get" method and always call that, but it would be kind of cool to be able to get this working.

    Currently, I'm using the following 'getValue' method

    javascript Code:
     
    Foo.prototype.getValue = function(inString)
    {
    	var result = this[inString];
     
    	if (result == undefined)
    	{
    		// result doesn't exist yet, create it
    		result = {totalBytes: 0, value: inString};
    		this[inString] = result;
    	}
     
    	// increment total bytes
    	result.totalBytes += inString.length;
     
    	return result;
    }


    Assume that object Foo doesn't currently have a "bill" property. What I would like to happen when a user does something like the following would be to silently initialize the "bill" record and return it rather than returning "undefined"

    console.log('Foo.bill: ' + Foo.bill)

    Current result: undefined
    Desired result: {totalBytes: 4, value: "bill"}

    As I said, I'm not sure this is possible, but I thought I'd see if anyone knows of a nifty hack.

    Thanks in advance
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Some of the newer JavaScript engines support getters and setters, but for web development it's not practical right now because some of the major browsers don't support it and there is no way to even simulate it in those browsers.

    edit: looks like I was mistaken, support for getters and setters appears to have been added to IE 9.
    Last edited by E-Oreo; May 2nd, 2013 at 11:44 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Posts
    569
    Rep Power
    70
    Originally Posted by E-Oreo
    Some of the newer JavaScript engines support getters and setters, but for web development it's not practical right now because some of the major browsers don't support it and there is no way to even simulate it in those browsers.

    edit: looks like I was mistaken, support for getters and setters appears to have been added to IE 9.
    Getters and setters didn't work in this situation, because the property being accessed is undefined until the actual call is made. I think this would require a modification to the Javascript language itself, allowing for the definition of null methods, which could be called, as a last resort, before returning 'undefined'.

    After playing around with this for several hours, I decided to just go with my 'getValue' method. Not quite as snazzy, but it does exactly what I want...
    Last edited by ktoz; May 3rd, 2013 at 08:37 AM.

IMN logo majestic logo threadwatch logo seochat tools logo