为什么我的JavaScript构造函数返回默认属性值而不是修改后的默认值? [英] Why does my JavaScript constructor return default property value instead of the modified one?

查看:63
本文介绍了为什么我的JavaScript构造函数返回默认属性值而不是修改后的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Material Steppers一起开发一个小型AngularJS应用程序.

I am working on a small AngularJS application with with Material Steppers.

我必须从页面的两个部分中选择项目,并且仅当两个部分中的项目属于以下类别时,才返回 true id( categoryID )1.

I have to select items from two sections of the page and return true only if the items from both sections belong to the category with id (categoryID) 1.

class Controller {
    constructor($mdStepper, validationService) {
        this.$mdStepper = $mdStepper;
        this.isTriggerA = true;
        this.isTriggerB = true;
        this.clickedStepNumber = 0;

        static get $inject() {
            return [
                '$mdStepper',
                'validationService'
            ];
        }

        getCurrentStep() {
          this.steppers = this.$mdStepper('stepper');
          const steps = this.steppers.steps;
          steps.forEach((el, index) => {
            let step = this.steppers.steps[index];
            if (step.isClicked()) {
              this.clickedStepNumber = step.stepNumber;
            }
          });
        }

        checkCategory() {
          this.getCurrentStep();
          if (this.filter.provider) {
            let categoryID = parseInt(this.filter.category.id, 10);

            console.log('Cid: ' + categoryID);

            if (categoryID !== 1) {
              this.isTestPassed = false;
            } else {
              if (parseInt(this.clickedStepNumber, 10) === 1) {
                this.isTriggerA = true;
                console.log('Step: ' + this.clickedStepNumber);
                console.log("A1: " + this.isTriggerA);
                console.log("B1: " + this.isTriggerB);
              }

              if (parseInt(this.clickedStepNumber, 10) === 2) {
                this.isTriggerB = true;
                console.log('Step: ' + this.clickedStepNumber);
                console.log("A2: " + this.isTriggerA);
                console.log("B2: " + this.isTriggerB);
              }

              if (this.isTriggerA === true && this.isTriggerB === true) {
                this.isTestPassed = true;
              } else {
                this.isTestPassed = false;
              }
            }
        }
    }
}

我也有这项服务:

export default class validationService {
  constructor() {
    this.isTestPassed = true;
  }
}

问题:

假设我处于两个我的if语句都返回 false 的情况.从这一点出发,我在第一个块(步骤)中进行了更改,以使其返回 true .

Supose I am in a situation where both my if statements return false. From this point, I make a change in the first block (step) so that it returns true.

第二个块中的if不能记住"第一个块中if所返回的值.它返回到构造函数顶部的值.

The if in the second block does not "remember" the value returned by the if in the first block. It returns to the value at the top of the constructor.

这意味着下面的代码块

if (this.isTriggerA === true && this.isTriggerB === true) {
   this.isTestPassed = true;
} else {
   this.isTestPassed = false;
}

早早返回true-在 this.isTriggerA this.isTriggerB 都成立之前.

returns true to early - before this.isTriggerA and this.isTriggerB are both true.

我需要脚本始终意识到两个块的状态().

I need the script to be aware of the "state" of both blocks at all time.

注意:我无法在代码开头将 isTriggerA isTriggerB 设置为false,如果未选择任何内容,则需要返回true也是.

Note: I can not set isTriggerA and isTriggerB to false at the beginning my code, I need to return true if nothing is selected too.

我该如何解决?

推荐答案

在应用程序顶部,您需要声明:

At the top of your application you are declaring:

    this.isTriggerA = true;
    this.isTriggerB = true;

这意味着两者的默认值为true,因此如果您的if条件都为

That means that the default value for both is true, so if both your if conditions:

if (parseInt(this.clickedStepNumber, 10) === 1) 

还有

if (parseInt(this.clickedStepNumber, 10) === 2)

为假,最后一个语句为 this.isTriggerA === true&&this.isTriggerB === true 将始终为true.

Are false, the last statement this.isTriggerA === true && this.isTriggerB === true will always be true.

要默认情况下不执行此操作,请在代码开头将 isTriggerA isTriggerB 设置为false

To do not execute this by default, set isTriggerA and isTriggerB to false at the beginning of your code

由于您的情况是:

我必须从页面的两个部分中选择项目,并且仅当两个部分中的项目都属于ID为(categoryID)1的类别时才返回true.

I have to select items from two sections of the page and return true only if the items from both sections belong to the category with id (categoryID) 1.

默认情况下将这两个值保持为真是没有意义的

There is no sense in to keeping those 2 values as true by default

这篇关于为什么我的JavaScript构造函数返回默认属性值而不是修改后的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆