Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How about this programming style in javascript?

Tags:

javascript

    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);
                }
        }
like image 441
Totty.js Avatar asked Mar 12 '26 21:03

Totty.js


1 Answers

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.

like image 59
Eli Avatar answered Mar 14 '26 09:03

Eli