javascript - js 对象的初始化不适用于特定变量名称

标签 javascript

我正在开发一个用 php 和一些 HTML 部分编写的 webprojekt。在生成页面的过程中,我使用自己的 id 生成器,它返回网页上唯一的 id。它们用于识别 HTML 对象并根据我的 js 类初始化 js 对象。

例如:

var s = new Actuator('test actuator')

问题是,如果 js 对象在特定字符(如“s”)上初始化,则该对象的功能将无法访问。如果我尝试像这样调用 Room 类的函数 setOn():

s.setOn()

出现消息setOn不是一个函数。但如果该对象在其他字符串上初始化,它就可以工作。

另一点是,如果我抑制生成器类中的 id ,其他字母也会出现同样的问题。

编辑: 类执行器:

/**
 * The Actuator class symbolizes the actuator on the web page and
 * handles the communication with the server.
 */

function Actuator(houseCode, deviceCode, user, modul_id, rowID) {
    this.houseCode = houseCode;
    this.devideCode = deviceCode;
    this.user = user;
    this.fmod_id = modul_id;
    this.frowID = rowID;
    //alert(rowID);


    /**
     * The function setOn sends the command to switch the actuator to the given state and handles the style changes.
     *
     * @param token
     * @param idOn
     * @param idOff
     */
    this.setOn = function (token, idOn, idOff) {
        var date = Date.now();
        asyncKommunikation("com.php?action=1&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&user=" + user + "&status=1&date=" + date);
        document.getElementById(idOn).className = 'btn btn-success btn-sm';
        document.getElementById(idOff).className = 'btn btn-danger btn-sm active';
    };

    /**
     * The function setOff sends the command to switch the actuator to the given state and handles the style changes.
     *
     * @param token
     * @param idOn
     * @param idOff
     */
    this.setOff = function (token, idOn, idOff) {
        var date = Date.now();
        asyncKommunikation("com.php?action=1&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&user=" + user + "&status=0&date=" + date);
        document.getElementById(idOn).className = 'btn btn-success btn-sm active';
        document.getElementById(idOff).className = 'btn btn-danger btn-sm';
    };

    /**
     * The function setAktiv sends the command to switch the actuator to the given state and handles the style changes.
     *
     * @param token
     * @param idOn
     * @param idOff
     * @param DidOn
     * @param DidOff
     */
    this.setAktiv = function (token, idOn, idOff, DidOn, DidOff) {
        var date = Date.now();
        asyncKommunikation("com.php?action=2&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&status=1&date=" + date);
        document.getElementById(idOff).className = 'btn btn-danger btn-sm active';
        document.getElementById(idOn).className = 'btn btn-success btn-sm';
        document.getElementById(DidOn).removeAttribute("disabled");
        document.getElementById(DidOff).removeAttribute("disabled");
    };

    /**
     * The function setInaktiv sends the command to switch the actuator to the given state and handles the style changes.
     *
     * @param token
     * @param idOn
     * @param idOff
     * @param DidOn
     * @param DidOff
     */
    this.setInaktiv = function (token, idOn, idOff, DidOn, DidOff) {
        var date = Date.now();
        asyncKommunikation("com.php?action=2&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&status=0&date=" + date);
        document.getElementById(idOn).className = 'btn btn-success btn-sm active';
        document.getElementById(idOff).className = 'btn btn-danger btn-sm';
        document.getElementById(DidOn).setAttribute("disabled", "disabled");
        document.getElementById(DidOff).setAttribute("disabled", "disabled");

    };

    /**
     * The function addGroup sends the command to add the group
     * and reloads the page.
     *
     * @param token
     * @param Obj
     */
    this.addGroup = function (token, Obj) {
        var date = Date.now();
        var sObj = document.getElementById(Obj);
        var val = readSelect(sObj);
        asyncKommunikation("com.php?action=3&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&group_id=" + val + "&date=" + date);
        timmedreload();
        //location.reload();
    };

    /**
     * The function delGroup sends the command to delete the actuator-group association
     * and reloads the page.
     *
     * @param token
     * @param val
     */
    this.delGroup = function (token, val) {
        var date = Date.now();
        asyncKommunikation("com.php?action=4&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&group_id=" + val + "&date=" + date);
        timmedreload();
        //location.reload();
    };

    /**
     * The function delGroup sends the command to delete the actuator-group association
     * and reloads the page.
     *
     * @param token
     * @param Obj
     */
    this.changeRoom = function (token, Obj) {
        var date = Date.now();
        var sObj = document.getElementById(Obj);
        var val = readSelect(sObj);
        asyncKommunikation("com.php?action=5&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&room_id=" + val + "&date=" + date);
    };

    /**
     * The function newActuator adds a new Actuator with the given attributes.
     *
     * @param token
     * @param objDeviceCode
     * @param objHouseCode
     * @param objDropdown
     */
    this.newActuator = function (token, objDeviceCode, objHouseCode, objDropdown) {
        var date = Date.now();
        var sObj = document.getElementById(objDropdown);
        var val = 1;
        if(sObj.getAttribute("disabled") != "disabled"){
            val = readSelect(sObj);
        }
        var fieldDeviceCode = document.getElementById(objDeviceCode);
        var fieldHouseCode = document.getElementById(objHouseCode);

        var fDeviceCode = fieldDeviceCode.value;
        var fHouseCode = fieldHouseCode.value;

        if(fDeviceCode == "" || fHouseCode == ""){
            if( fHouseCode == ""){
                $(fieldHouseCode).css('border-color', 'red');
            }else{
                $(fieldHouseCode).css('border-color', '');
            }
            if(fDeviceCode == ""){
                $(fieldDeviceCode).css('border-color', 'red');
            }else{
                $(fieldDeviceCode).css('border-color', '');
            }
            return false;
        }

        //alert("com.php?action=6&token=" + token + "&device=" + fDeviceCode + "&housecode=" + fHouseCode + "&room_id=" + val + "&date=" + date);
        asyncKommunikation("com.php?action=6&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + fDeviceCode + "&housecode=" + fHouseCode + "&room_id=" + val + "&date=" + date);

        //this.devideCode = fDeviceCode;
        //this.houseCode = fHouseCode;

        //this.changeRoom(token, objDropdown);
        timmedreload();
    };

    /**
     * The delActuator function sends the command to delete the actual actuator.
     *
     * @param token
     */
    this.delActuator = function (token) {
        //alert(this.frowID);
        var date = Date.now();
        asyncKommunikation("com.php?action=7&mod_id=" + this.fmod_id + "&token=" + token + "&device=" + this.devideCode + "&housecode=" + this.houseCode + "&date=" + date);
        rowSlideUp(this.frowID);
        //timmedreload();
    };
}

页面上执行的代码:

var f = new Actuator(10000 ,1 , 'User' , '2', 'p' ); 

document.getElementById('g').addEventListener( 'click' , function(){ f.setOn( 'e6cf962d9c88d403ca04d944524123d4476b4bbe88e5c81fa28d6895572d2d55' , 'g', 'h') } );

document.getElementById('h').addEventListener( 'click' , function(){ f.setOff( 'e6cf962d9c88d403ca04d944524123d4476b4bbe88e5c81fa28d6895572d2d55' , 'g', 'h') } );

document.getElementById('i').addEventListener( 'change' , function (){ f.changeRoom('513e160dac296b5540d61d400c6c3c4cbbc8078f8833dce7231806ab27f0c1d3' , 'i')} );

document.getElementById('l').addEventListener( 'click' , function(){ f.addGroup( 'f7e02646a6c2b4f4748fe7db60fa6c892d3a07c7685a901a218ace225d2d1348' ,'k') } );

document.getElementById('m').addEventListener( 'click' , function(){ f.setAktiv( '8e2770eb26c04913c21ebbad603f19b41a7a0e85951af0a1cfb9cae9780f7b74' , 'm', 'n' , 'g' , 'h') } );

document.getElementById('n').addEventListener( 'click' , function(){ f.setInaktiv( '8e2770eb26c04913c21ebbad603f19b41a7a0e85951af0a1cfb9cae9780f7b74', 'm', 'n' , 'g' , 'h') } );

document.getElementById('o').addEventListener( 'click' , function (){ f.delActuator( 'ac526fb7f65c1f9519ce0c1834bbc7a67677b2fc4c8e6ee59c7cb4f630e6c13e' ) } );

最佳答案

我们需要查看您的全局代码以找​​出出现此问题的原因。很可能您是在全局上下文中设置这些变量,因此它们并不是您尝试使用它们时显示的值。

关于javascript - js 对象的初始化不适用于特定变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33953588/

相关文章:

javascript - 小袋鼠和原型(prototype)错误

javascript - 浏览器的点对点库

javascript - 通过 setInterval 添加输入不起作用

javascript - React中如何用hooks实现componentDidMount才能符合EsLint规则 "react-hooks/exhaustive-deps": "warn"?

javascript:确保元素始终出现在顶部

javascript - 为什么一点击输入框就触发事件?

javascript - 从 HTML 调用 Node 请求

javascript - 根据名称选择jquery中的输入字段

javascript - 在 componentDidMount 上触发转换

javascript - Microsoft Graph API,创建附件不起作用?