简体   繁体   中英

What is Intellisense telling me about JavaScript var scope in nested functions?

Using VS2013. Either Intellisense is not working correctly or (more likely) it is telling me something deep about scope that escapes me:

function Outer() {
    var aaa = 1;
    function Inner() {
      a
    }
}

As I am typing the a in Inner, Intellisence shows aaa, as expected.

function Outer() {
    var aaa = 1;
    return;
    function Inner() {
      a
    }
}

Here, Intellisense does not show aaa. Why? Has it decided that as I haven't called Inner, aaa will go out of scope? That's a harsh decision, but it seems that's what it's doing because with

function Outer() {
    var aaa = 1;
    Inner();
    return;
    function Inner() {
      a
    }
}

Intellisense will again display aaa. This is correct behaviour?

I am new to JS. I come from C#, where scope does not change between dev and runtime. I understand that nested functions make the scope unpredictable at runtime. Is Intellisense trying to protect me from that?

Thanks

This is a very interesting question. Without having worked on Intellisense for JavaScript, I can't answer with authority; but I think I have a guess.

My understanding actually comes from Marijn Haverbeke , a prominent JavaScript developer most notable for CodeMirror (which is the text editor used in some important dev tools, including Chrome's web inspector if I'm not mistaken).

Haverbeke is working on a type inference engine for JavaScript called Tern . In a blog post about Tern , he acknowledges the awesomeness of Visual Studio's JS type inference (emphasis below is mine):

Since VS 11, 'Intellisense' for JavaScript is nothing short of amazing. It works, as far as I understand it, by actually running your code in a magic invisible way, instrumenting the Chakra engine to ignore I/O and cut off loops that run too long or recursion that goes too deep, and then inspecting the resulting JavaScript environment to find out what actual types were created for a given variable or expression. This makes it amazingly accurate, even when you're doing very odd things with your types. Downside is that it'll sometimes not be able to run the code that you need a completion for with its actual input types (it needs to find a code path leading to that code, which can be tricky), and thus fail to provide completions.

I don't know if he has friends who work on Visual Studio or what, but this does seem to be corroborated by what you're seeing. If Intellisense is actually executing the JS in a sort of quarantined environment, that would explain why returning before Inner is defined would prevent type inference from working.

In other words, Intellisense isn't actually doing static analysis on your JavaScript (if it were, a dead code path shouldn't stop it from understanding theoretical scope). It's actually running the JavaScript to determine types. So (if Haverbeke is right) dead code paths are dead ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM