【问题标题】:Get variables outside of method (object)获取方法(对象)之外的变量
【发布时间】:2018-07-23 17:32:48
【问题描述】:

我希望在方法之外检索变量,但 OOP 对我来说是新事物,我有点卡住了......

const Reservation = {
    play: document.getElementById('Reservation'),
    reset: document.getElementById('annuler'),

    launch_functions_sessionStorage: function () {
        this.onReservation();
        this.displayIfChecked();
        this.set_Timer();
    },

    displayIfChecked: function () {
        SOMETHING
    },

    onReservation: function () {
        $('#Reservation, .signature',).click(function () {
            console.log();
            if (SOMETHING) {
                let checked = 1;
            } else {
                let checked = 0;
            }
        });
    },

    set_Timer: function () {

        Reservation.play.addEventListener('click', function(){
            if('I would like to get 'checked'){}
        });
    }
};
Reservation.launch_functions_sessionStorage();

我尽量保留重要信息以供您理解。 同时对不起我的英语...... 非常感谢

【问题讨论】:

  • 问题出在哪里?
  • 如何在 set_timer: 函数上检查我的变量?
  • 通过作为参数传递?我不确定我是否理解这里的问题。
  • 我想它可以解决我的问题。同时我不知道如何在方法之外调用变量。就是这样^^
  • 类似Reservation.set_time( params... ) ?

标签: javascript oop variables methods


【解决方案1】:

这是你要找的吗?

const Reservation = {
    play: document.getElementById('Reservation'),
    reset: document.getElementById('annuler'),

    launch_functions_sessionStorage: function () {
        this.onReservation();
        this.displayIfChecked();
        this.set_Timer();
    },

    displayIfChecked: function () {
        SOMETHING
    },

    onReservation: function () {
        $('#Reservation, .signature',).click(function () {
            console.log();
            if (SOMETHING) {
                let checked = 1;
            } else {
                let checked = 0;
            }
        });
    },
    varThatCanBeCheckedInSetTimer : false,
    set_Timer: function () {
        Reservation.play.addEventListener('click', function(){
            if(varThatCanBeCheckedInSetTimer){
                //your stuff that will happen only if you set varThatCanBeCheckedInSetTimer  to true
             }
        });
    }
};

你会像这样使用它

Reservation.varThatCanBeCheckedInSetTimer = true;
Reservation.set_Timer();

【讨论】:

  • @LudovicWEBER 很高兴能提供帮助,很高兴接受答案并 +1 ;)
猜你喜欢
  • 2022-08-23
  • 2013-01-19
  • 1970-01-01
  • 2011-07-30
  • 2012-11-06
  • 1970-01-01
  • 2023-03-23
  • 2019-02-14
  • 1970-01-01
相关资源
最近更新 更多