8

When creating a JS object and defining some accessors, I found that I couldn't get the intellisense after this.

Example code :

function Obj(foo) {
    this.foo = foo;
}

Obj.prototype = {
    get bar() {
        return this.// No intellisense here
    },
    set bar(val) {
        this.foo = val
    }
};

But creating a method with Obj.prototype.test = function () {...} will get me the intellisense.

Is there any way to replicate the same lexical analysis for this case with the user settings, or is it an actual flaw / bug ?

Edit: here is an image of what VSCode shows : VSCode intellisense

As you can see, it only shows previously used words, and nothing else.

Edit (03/04/2019): After trying again today, with v1.32.2, I noticed VSCode is able to suggest the bar property, but still not foo. So something was improved, but my issue not solved.

Seblor
  • 6,947
  • 1
  • 25
  • 46
  • 1
    Hmmm, this might be a limitation of Visual Studio Code. My Visual Studio 2015 offers intellisense here. See: http://www.imageno.com/x9gcirtzri72pic.html – Gyum Fox Aug 11 '17 at 12:23
  • Interesting. I think I will report that to the Github issues then. – Seblor Aug 11 '17 at 12:26
  • see updated image: http://www.imageno.com/jby8091zlcrypic.html – Gyum Fox Aug 11 '17 at 12:31
  • Thanks. I will update my question with an image so you cann see what I actually have – Seblor Aug 11 '17 at 12:34
  • [Don't assign objects to `.prototype`](https://stackoverflow.com/questions/17474390/defining-a-javascript-prototype). Not sure whether that's the reason VSC doesn't recognise it as a method. – Bergi Aug 11 '17 at 13:40
  • @Bergi What do you suggest for creating accessors that can be read easily, and does not need a dozen of lines ? – Seblor Aug 11 '17 at 13:48
  • 1
    @Seblor `Object.defineProperty(Obj.prototype, "bar", {get(){ … }, set(){ … }, configurable: true})` – Bergi Aug 11 '17 at 13:50
  • It still does not shows the object properties in the completion box, but I agree it is safer. – Seblor Aug 11 '17 at 14:36

2 Answers2

2

Microsoft recommends restarting VS to see if it helps. Or you could be missing type declarations if this happens with other libraries besides objects.

There's a link here that has some troubleshooting tips that you can follow.

There's a more specific link here that deals with JavaScript in Code.

Also, Code might not show it because there already is something showing up related to it. It sees that it already showed foo. Try renaming it and see if that helps

Jimenemex
  • 3,104
  • 3
  • 24
  • 56
0

Use jsdoc

function Obj(foo) {
  this.foo = foo;
}
  /** @typedef {{foo:any,bar:any}} ObjThis*/

Obj.prototype = {
  /** @this {ObjThis}*/
  get bar() {
      return this.bar// No intellisense here
  },
  /** @this {ObjThis}*/
  set bar(val) {
      this.foo = val
  }
};
Hossein Mohammadi
  • 1,388
  • 11
  • 16
  • I know it's possible to do it with JSDoc, but since this does not depend on runtime, it should be working with the code only. However, the current version of VSCode recognizes the `bar` property as part of the `Obj` class. I'll update my question, but the problem of `foo` not being listed is still an issue. Edit: scratch that, the behavior did not change since April 2019 – Seblor Mar 13 '21 at 22:18