3
    var RootComponent = {

        init: function(options){
            options = jQuery.extend({name: 'Root'}, options);

            this.run('ContainerComponent.init')(options);
        }

     }

To this is then applied a new method, called "run". And this run the method found in the path with the this context. Now what issues do you think I might have? thanks

        klass.run = function(path){
                var that = this;
                return function(){
                 // here will be calculated the path, based on the input, this is just an hard coded example...
                    that.sb.klasses['ContainerComponent']['init'].apply(that, arguments);
                }
        }
4
  • that.sb.klasses['ContainerComponent']['init'] can and should be written as: that.sb.klasses.ContainerComponent.init Commented Jan 18, 2011 at 8:41
  • it's the same, but when this will not be hardcoded in the place of ContainerComponent will be another string, depending by the component I need... Commented Jan 18, 2011 at 18:26
  • Can you give more context, such as how you'd use this? You've got some gaps in your code, like where that.sb.klasses is defined, or klasses itself, or how RootComponent hooks up with klass. Commented Feb 3, 2011 at 8:10
  • Look at bootstrap-js.net It seemes, that they do similar thing, and it also seemes, they have some experience with their library. Commented Feb 6, 2011 at 10:52

1 Answer 1

1

Are you going to be using this to modify only objects you have control of? If you are using this on object that someone else may provide, you will be overriding their possible implementation of run.

You could override their implementation of run anyway, but then you can about guarantee breaking their code.

You could check if run already exists on that object, but now your code has a dependency on the structure of the incoming object, and if you can't implement your method on their object, their code will continue to work, but yours could potentially fail (because you would be using their implementation and not your own.)

It's for these reason that its always recommended to never modify objects you don't own. Like I said, if you have control over whether or not these methods follow your pattern, then it's not an issue. But if this code is part of an environment-agnostic framework, issues can arise. Better to have a helper method that acts on the object, rather than extending the object with unexpected functionality.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.