Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an object like this

initialize : {  
    method : function() {
        this.__. //--> do another thing
        ... //--> so many
        this.__.params. //--> do another thing
        ... //--> so many
    }           
}

and the method of this object call by another function. As you can see, we have many lines that refer to this.__ and this.__. params, and for preventing this, I declare local variables T and P like below

initialize : {  
    method : function() {
        var T = this.__, P = T.params;
        T. //--> do another thing
        ... //--> so many
        P. //--> do another thing
        ... //--> so many
    }           
}

as I mentioned before the method calls by another function called caller.

Is it possible to not declare locale variables in the method function and instead define them in scope of the caller function and pass it to the method ?

share|improve this question

closed as off-topic by 200_success Apr 8 '14 at 1:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
If I understand correctly, you have another object which is calling method as if it were its own method (and therefore this references the other object). I'm assuming that since you're trying to simplify this bit of code, it's a user-extendable part of some library. I don't really see any problem with it as-is (except for the __ name, which is a bit odd). I guess I'd need more context to know why you want to simplify it. You certainly shouldn't have the caller define the variables for you (which is impossible anyway), but I don't see why you can't just use normal parameters? – Dave Oct 13 '13 at 11:58
    
If you can tell us what this and this.__ is, maybe we can provide another approach. – Joseph the Dreamer Oct 13 '13 at 12:07
    
Thanks, in fact I just write a framework base on aspect oriented programming for the developers and it's include a lot of components and I try to simplify it, for example I told the developers that follow as example number two because it reduces the code size that must be send from server to client (because of the problem of bandwidth), and after a while I thought if is it possible to declare locale variable in another scope and pass it to another scope. anyway Thanks for you help. – AKZ Oct 13 '13 at 12:18
    
If you set up your server to gzip resources (which you should), this isn't an issue anyway. Also you should be minifying your scripts before publishing them (e.g. using the closure compiler). I would still suggest that parameters are the way to go (if not what you already have), and you shouldn't put a lot of effort into saving 10 characters. – Dave Oct 13 '13 at 12:21

1 Answer 1

Not much to review ;)

  • As @Dave mentioned, __ is a bit of an odd named, for sure it does not give away what it does
  • You might consider storing __.params separately in this.parameters or this.options

I would need more code to give deeper, insightful comments ;)

share|improve this answer
    
Hypothetical code is usually unreviewable, which is why we consider such questions off-topic. – 200_success Apr 8 '14 at 1:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.