skip to Main Content

the question seems very primitive but i am new to javascript and vuejs. as shown in the code posted below,i am creating a class DigitizePolygonInteractions that extends MapBuilder. the constructor receives two parameters moduleName and mapInstance.

i want to assign the parameter mapInstance that is passed to the constructor to a local private variable named mapInstance as well. as for the code posted below, visual-studio codes does not highlight the let mapInstance when the following line of code
is added this.mapInstance = mapInstance, which indicates that this.mapInstance is not used.

in java, given the code posted below, for the statement this.mapInstance = mapInstance, this.mapInstance refers to let mapInstance while mapInstance refers to mapInstance that is passed to the constructor

how can i achieve the same in javascript please

code:

let instance;
let mapInstance //<==should be the local private variable

export class DigitizePolygonInteractions extends MapBuilder {
 /** 
 *  @param { string } moduleName like 'map-sen2bee'
 */
  constructor(moduleName,mapInstance) {
    super(moduleName);
    instance = this
    this.mapInstance = mapInstance //<==============here
    

}

2

Answers


  1. let instance;
    let mapInstance;
    
    export class DigitizePolygonInteractions extends MapBuilder {
      constructor(moduleName, _mapInstance) {
        super(moduleName);
        instance = this;
        mapInstance = _mapInstance;
      }
    }
    

    However, a better approach would be:

    export class DigitizePolygonInteractions extends MapBuilder {
      constructor(moduleName, mapInstance) {
        super(moduleName);
        this.instance = this;
        this.mapInstance = mapInstance;
      }
    }
    
    Login or Signup to reply.
  2. let instance;
    let mapInstance // this variable will be scoped to the file or module.  
    
    
    export class DigitizePolygonInteractions extends MapBuilder {
     /** 
     *  @param { string } moduleName like 'map-sen2bee'
     */
      constructor(moduleName, _mapInstance) {
        super(moduleName);
        instance = this
        this.mapInstance = _mapInstance; // this.mapInstance is a property on the object that this class will generate. 
        mapInstance  = _mapInstance; // mapInstance is a variable that exists outside this class. Basically it has nothing to do with this class.
        
       }
    
    
    

    I haven’t worked with classes that much, but I can try to explain it with a very simple example below;

    
    let name;
    
    class Person{
      constructor(_name, age){
        // 'this' here refers to the object that will be created when this class is instantiated (see below)
       this.name = _name;
       name = _name;
       this.age = age;  
    } 
    }
    
    const boy1 = new Person('jack', 21);
    
    console.log(boy1) // {name: 'jack', age: 21}
    console.log(name) // jack 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search