开发者

How about this programming style in javascript?

开发者 https://www.devze.com 2023-02-05 18:45 出处:网络
var RootComponent = { init: function(options){ options = jQuery.extend({name: \'Root\'}, options); this.run(\'Containe开发者_Go百科rComponent.init\')(options);
    var RootComponent = {

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

            this.run('Containe开发者_Go百科rComponent.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);
                }
        }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号