SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Decorating an object

    Hi,

    I'm trying to create a simple object decorator in JavaScript. Since I'm only interested in decorating a couple of methods, I don't want to copy the whole interface of the object to the decorator manually. Ideally, I would like the decorator to blindly forward all its method calls to the object it decorates EXCEPT those few methods I need to alter.

    Here's an example:

    Code:
    // The original interface
    
    MyObject.doSomething1(); // I want to alter this one...
    MyObject.doSomething2(); // ... and this one, too
    MyObject.doSomethingElse1(); // I don't care about this one...
    MyObject.doSomethingElse2(); // ... nor this one or any other
    
    // The decorator
    
    var deco = new Decorator(new MyObject());
    deco.doSomething1(); // This calls the decorator's doSomething1()
    deco.doSomething2(); // This calls the decorator's doSomething2()
    
    deco.doSomethingElse1(); // This should call the original object's doSomethingElse1()
    deco.doSomethingElse2(); // This should call the original object's doSomethingElse2()
    Something similar to __call() in PHP would be very useful here.

    Is there a way to do what I want?

  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)
    You should be able to assign an empty function to the methods that you wish to disable.

    Code javascript:
    // The original interface
    function MyObject() {
    	this.doSomething1 = function () {
    		console.log('Do something 1.');
    	}
    	this.doSomething2 = function () {
    		console.log('Do something 2.');
    	}
    	this.doSomethingElse1 = function () {
    		console.log('Do something else 1.');
    	}
    	this.doSomethingElse2 = function () {
    		console.log('Do something else 2.');
    	}
    	return this;
    }
     
    // The decorator
    function Decorator(obj) {
    	obj.doSomething1 = function () {
    		console.log('Decorated do something 1.');
    	}
    	obj.doSomething2 = function () {
    		console.log('Decorated do something 2.');
    	}
    	obj.doSomethingElse1 = function () {
    	}
    	obj.doSomethingElse2 = function () {
    	}
    	return obj;
    }
     
    var deco = new Decorator(new MyObject());
    deco.doSomething1(); // This calls the decorator's doSomething1()
    deco.doSomething2(); // This calls the decorator's doSomething2()
     
    deco.doSomethingElse1(); // This should call the original object's doSomethingElse1()
    deco.doSomethingElse2(); // This should call the original object's doSomethingElse2()
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to disable anything...

  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)
    Then just leave them untouched.

    Code javascript:
    // The decorator
    function Decorator(obj) {
        obj.doSomething1 = function () {
            console.log('Decorated do something 1.');
        }
        obj.doSomething2 = function () {
            console.log('Decorated do something 2.');
        }
        return obj;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @Pmw57, A decorator object doesn't overwrite methods. It runs them.

    @ReeD, What you're looking for is Method Overloading. PHP's __Call does this nicely, however JS lacks it.

    So, really, you'll have to duplicate each function name. For each function that you want to decorate, modify.

    For each function that you don't want to decorate, simply return the original object's function.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •