views:

36

answers:

1

I have a number of files with contents like this:

function hello() {
    ...
    element1.text = foo.locale.lorem;
    element2.text = foo.locale.ipsum;
    ...
    elementn.text = foo.locale.whatever;
    ...
}

function world() {
    ...
    var label = bar.options.baz.blah;
    var toggle = bar.options.baz.use_toggle;
    ...
}

This could be written more efficiently, and also be more readable, by creating a shortcut to the locale object:

function hello() {
    var loc = foo.locale;
    ...
    element1.text = loc.lorem;
    element2.text = loc.ipsum;
    ...
    elementn.text = loc.whatever;
    ...
}

function world() {
    var options = bar.options.baz;
    ...
    var label = options.blah;
    var toggle = options.use_toggle;
    ...
}

Is there a simple way to detect occurrences of such duplication for any arbitrary object (it's not always as simple as "locale", or foo.something)? Basically, I wanna know where lengthy object references appear two or more times within a function.

Thanks!

A: 

Are you talking about something like LINT? e.g. something externally that can report such object references or internally like looping over the window object

mplungjan
Some external tool that would tell me "look here, perhaps you can optimize this".
AnC