如何将传递给构造函数的参数分配给同名的局部私有变量

huangapple go评论75阅读模式
英文:

how to assign parameter passed to constructor to local private variable with the same name

问题

代码中的问题似乎很基础,但我对JavaScript和Vue.js都很新手。如下所示,我正在创建一个扩展了MapBuilder的类DigitizePolygonInteractions,构造函数接受两个参数moduleNamemapInstance

我想将传递给构造函数的参数mapInstance分配给一个名为mapInstance的本地私有变量。至于下面的代码,当添加以下代码行时,Visual Studio Code不会突出显示let mapInstance,这表明this.mapInstance未被使用。

在Java中,给定下面的代码,对于语句this.mapInstance = mapInstancethis.mapInstance指的是let mapInstance,而mapInstance指的是传递给构造函数的mapInstance

请问如何在JavaScript中实现相同的效果?

代码

	let instance;
	let mapInstance //<==应该是本地私有变量
	
	export class DigitizePolygonInteractions extends MapBuilder {
     /** 
     *  @param { string } moduleName like 'map-sen2bee'
     */
      constructor(moduleName, mapInstance) {
        super(moduleName);
        instance = this;
        this.mapInstance = mapInstance; //<==============在这里
        
}

希望这可以帮助您理解代码。如果您有任何进一步的问题,请随时提出。

英文:

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

}

答案1

得分: 1

let instance;
let mapInstance;

export class DigitizePolygonInteractions extends MapBuilder {
  constructor(moduleName, _mapInstance) {
    super(moduleName);
    instance = this;
    mapInstance = _mapInstance;
  }
}
export class DigitizePolygonInteractions extends MapBuilder {
  constructor(moduleName, mapInstance) {
    super(moduleName);
    this.instance = this;
    this.mapInstance = mapInstance;
  }
}
英文:
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;
  }
}

答案2

得分: 1

let instance;
let mapInstance; // 此变量将作用域限定在文件或模块中。

export class DigitizePolygonInteractions extends MapBuilder {
  /**
   * @param { string } moduleName 例如 'map-sen2bee'
   */
  constructor(moduleName, _mapInstance) {
    super(moduleName);
    instance = this;
    this.mapInstance = _mapInstance; // this.mapInstance 是此类将生成的对象的属性。
    mapInstance = _mapInstance; // mapInstance 是存在于此类外部的变量。基本上与此类无关。
  }
}
let name;

class Person {
  constructor(_name, age) {
    // 'this' 这里指的是当此类被实例化时将要创建的对象(参见下面)
    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
英文:
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 

答案3

得分: 0

根据我所知,变量必须在类本身的上下文中才能通过this访问它们。不幸的是,我正在使用TypeScript而不是纯JavaScript,但在TypeScript中的一个工作示例如下:

let instance;

export class DigitizePolygonInteractions extends MapBuilder {

  private mapInstance: [Type] = {}; //<== 这是类的一个本地私有变量,可以通过'this'访问它

  constructor(moduleName, mapInstance) {
    super(moduleName);
    instance = this
    this.mapInstance = mapInstance 
    ...
英文:

As far as I know, the variables must be in the context of the class itself in order to access them via this. Unfortunately I am using typescript instead of pure javascript, but a working example in typescript would be:

let instance;

export class DigitizePolygonInteractions extends MapBuilder {

  private mapInstance: [Type] = {}; //<== is a local private variable of the class now and can be accessed via 'this'

  constructor(moduleName,mapInstance) {
    super(moduleName);
    instance = this
    this.mapInstance = mapInstance 
    ...

huangapple
  • 本文由 发表于 2023年7月6日 13:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定