我正在开发一个webprojekt,它是在php和一些HTML部分中写的。在页面生成期间,我正在使用我自己的id-generator,它返回在网页上唯一的id。它们用于识别HTML对象并根据我的js类初始化js对象。
例如:
var s = new Actuator('test actuator')
问题是,如果js对象在特定字符上初始化,如“s”,则此对象的功能不可访问。如果我尝试像这样调用Room类的函数setOn():
s.setOn()
messege 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' ) } );
答案 0 :(得分:0)
我们需要查看您的全局代码,以找出这是一个问题的原因。可能的情况是,您在全局上下文中设置这些变量,因此它们不是您尝试使用它们时看起来的值。
答案 1 :(得分:0)
function Room(s){
this.Name=s
}
Room.prototype.setOn=function(){alert("Working...")};
var s=new Room("test room");
s.setOn()
这将有效...但你可以保持它,以使它不干扰另一个变量:
!function(){
var TestRoom=new Room("test room")
}()