为什么我的变量在类函数中不可访问?

时间:2017-08-05 20:18:15

标签: javascript

我试图从类函数中更改构造函数中定义的变量的值。当我尝试更改或打印时,我得到的是:

  

未捕获的TypeError:无法设置属性' daysNumber'未定义的

我不确定,但我认为这可能会发生,因为以不寻常的方式调用此功能。

我根据构造函数中的选项调用函数。因此,如果其中一个选项是周末,则调用的函数为 setWeekends

setWeekends 函数内部发生错误。

"use strict";

class Calendar{
    constructor(element, params){
        this.element = element;
        this.params = params;
        this.parseParams();
        this.date = new Date();
        this.days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
        this.teachingHoursRange = ['7:10-7:55', '8:00-8:45', '8:50-9:35', '9:45-10:30', '10:50-11:35', '11:40-12:25', '12:30-13:15', '13:20-14:05', '14:10-14:55', '15:00-15:45'];
        // How many days will be displayed
        this.daysNumber = 5;
        // How many hours will be displayed
        this.teachingHoursNumber = 10;
        this.init();
    }

    init(){
        // Some important code
    }

    parseParams(){
        // Look for all parameters in this.params
        for (let key in this.params){
            // Make the first char capitalize
            let capitalizedKey = key.charAt(0).toUpperCase() + key.substr(1).toLowerCase();
            let functionName = "set" + capitalizedKey;

            // Look for member function set by function name
            let fn = this[functionName];

            // If fn is function, call it
            if (typeof fn === 'function') fn(this.params[key])

        }
    }

    setWeekends(value){
        if(value === true)
            this.daysNumber = 7
    }
}

这是我称之为此类的源代码。它位于不同的.js文件中。

let calendar = new Calendar("calendar", {
    weekends: true
});

3 个答案:

答案 0 :(得分:0)

将您的setWeekends方法更改为

setWeekends = (value) => {
    if(value === true)
        this.daysNumber = 7
}

答案 1 :(得分:0)

parseParams()中调用函数时,需要传递上下文。您可以使用.call()方法调用该函数来执行此操作。

parseParams(){
    // Look for all parameters in this.params
    for (let key in this.params){
        // Make the first char capitalize
        let capitalizedKey = key.charAt(0).toUpperCase() + key.substr(1).toLowerCase();
        let functionName = "set" + capitalizedKey;

        // Look for member function set by function name
        let fn = this[functionName];

        // If fn is function, call it
        if (typeof fn === 'function') fn.call(this, this.params[key])

    }
}

答案 2 :(得分:0)

试试这个

parseParams(){
     let vm = this;
        // Look for all parameters in this.params
        for (let key in this.params){
            // Make the first char capitalize
            let capitalizedKey = key.charAt(0).toUpperCase() + key.substr(1).toLowerCase();
            let functionName = "set" + capitalizedKey;

            // Look for member function set by function name
            let fn = this[functionName];

            // If fn is function, call it
            if (typeof fn === 'function') fn(vm, this.params[key])

        }
    }


setWeekends(vm, value){
        if(value === true)
            vm.daysNumber = 7
    }