detecting object-reference duplication across JavaScript files
Posted
by AnC
on Stack Overflow
See other posts from Stack Overflow
or by AnC
Published on 2010-04-22T06:46:50Z
Indexed on
2010/04/22
9:23 UTC
Read the original article
Hit count: 237
JavaScript
|duplicates
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!
© Stack Overflow or respective owner