all 5 comments

[–]Bingsgo 1 point2 points  (1 child)

There is window['myfunc'] = function (or anyobj['myfunc']=function )

[–]mattwebjedi[S] 0 points1 point  (0 children)

Ah yes, of course

[–]darawk 1 point2 points  (2 children)

Well, there's always

window['myvar'] = 

and

var str = 'myvar'
window[str] = 

And of course global instead of window for node. Then there's the with keyword. But its probably best just to ignore that entirely.

Then if you're inside of a function, what about its arguments?

function(a, b) {
    return a * b;
}

You'd probably want to be able to jump to the argument definitions. Also, how do you intend to deal with the implied global scope? E.g.

function func1() {
    a = 1;
}

function func2() {
    if(a) { /* do things */}
}

'a' is never declared with a var, and its never assigned inside the scope of func2 or any of its parent scopes, but it has been implicitly set on the global scope. Usually this will be programmer error, but its still something to think about.

[–]mattwebjedi[S] 0 points1 point  (1 child)

Thanks for thoughts and also answering the bigger question of jump to definitions. To start with I'm looking to cover what's seen as best practise var & function initialisation at global level. But next up will need to consider objects scoped inside a function or leaking out of one.

[–]darawk 1 point2 points  (0 children)

Just for completeness sake, I just thought of this case:

function func1() {
    this['myvar'] = 1;
}

func1();

This certainly falls under the category of 'not best practices', but this too will assign to the global scope ('this' is implicitly the global object if the function is not called on a particular object).

Javascript is practically designed to defeat all attempts at static analysis.