简体   繁体   English

在函数表达式中修改类变量

[英]Modifying class variable in function expression

I have some class variables which I want to modify in a function below but the value doesn't appear to stick. 我有一些要在下面的函数中修改的类变量,但该值似乎没有变化。

export class Welcome {

  myLatitude = 0;  //I want to modify this inside activate()
  myLongitude = 0;
  myMarkers = [
  {
        latitude: -27.451673,
        longitude: 153.043981
  }
  ];

  activate() {

    var lat = this.myLatitude; // I thought this was getting a reference to myLatitude.

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){ //additional function
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        var accuracy = position.coords.accuracy;

        lat = latitude; // Hoping to modify myLatitude here
        ...

However when I check the value of myLatitude again later, it still has a value of 0: 但是,当我稍后再次检查myLatitude的值时,它的值仍为0:

  @computedFrom('myLatitude')
  get myLat() {
    console.log(this.myLatitude);
    return `${this.myLatitude}`;
  }
lat = latitude; // Hoping to modify myLatitude here

Here, you assign the variable name lat to the object latitude, but you do not modify this.myLatitude . 在这里,您将变量名称lat分配给对象纬度,但没有修改this.myLatitude

I think you want to do: 我想你想做:

this.myLatitude = latitude;

You have to read a bit about variable scope and assignation: you have to understand the difference between a variable/name and the object it points to. 您必须阅读一些有关变量作用域和赋值的知识:您必须了解变量/名称与它所指向的对象之间的区别。

When you write var lat = this.myLatitude you create new independent variable. 当您编写var lat = this.myLatitude您将创建新的自变量。 lat is not a link to this.myLatitude 's value because it has a primitive one. lat不是this.myLatitude值的链接,因为它具有原始值。 Only objects in JavaScript are passed by reference. JavaScript中只有对象是通过引用传递的。

So in your case you can save a link to the context var self = this before getCurrentPosition method call and after change the value in this way self.myLatitude = latitude . 因此,在您的情况下,您可以在getCurrentPosition方法调用之前和以这种方式更改值self.myLatitude = latitude之后,保存到上下文的链接var self = this

Or you can use an arrow function: 或者,您可以使用箭头功能:

navigator.geolocation.getCurrentPosition(position => {
  ...
  this.myLatitude = latitude;
  ...
}

You don't need this sentence 你不需要这句话

lat = latitude;

Instead of that 代替那个

Try this: 尝试这个:

myLatitude=latitude;

You could define property myLatitude in a ES6 class constructor and in your concise method activate you could get myLatitude property and change its value using this keyword. 您可以在ES6类constructor定义属性myLatitude ,并在简洁的activate方法中使用this关键字获取myLatitude属性并更改其值。

Quick example: 快速示例:

 let Welcome = class Welcome { constructor(height, width) { this.myLatitude = 0; } activate() { this.myLatitude = '1'; // assign a new value alert('this.myLatitude is: ' + this.myLatitude); } }; let myWelcome = new Welcome(); myWelcome.activate(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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