Aici sunt cateva functii care mi s-au parut utile pentru teste si alte prostii. In principiu, cu ajutorul lor s-ar putea realiza un interpretor pentru comenzi in JavaScript scris tot in JavaScript. Daca nu va folosesc la nimic sau nu va dau nici o idee interesanta, sorry. 🙂
[sourcecode language=’jscript’]
/* ————— TESTING —————- */
//defining universal variables inside a function (example: ‘testVar’);
//I don’t know what this could be usefull for, but…
function def_var(varName, varValue){
if (varValue != null){
if (typeof(varValue) == “number”){
eval(‘window.’ + varName + ‘= ‘ + varValue)
}else{
eval(‘window.’ + varName + ‘= “‘ + varValue + ‘”‘)
}
}else{
eval(‘window.’ + varName + ‘= new String(“”)’)
}
}
//the same as the above but the variables
//are created as objects (example ‘testVar.value’)
function createVar(value){
this.value= value
}
function defvar(varName, varValue){
if (varValue != null){
if (typeof(varValue) == “number”){
eval(varName + ‘= new createVar(‘ + varValue + ‘)’)
}else{
eval(varName + ‘= new createVar(“‘ + varValue + ‘”)’)
}
}
else{eval(varName + ‘= new createVar(“”)’)}
}
//use this to destroy the objects which are no longer needed
function clearvar(varName){
eval(varName + ‘= null’)
}
//defining the ‘function’ object and a method of it: exec
function execFunc(){
eval(this.fun)
}
function defun(funName){
this.fun= funName //or the set of comands;
this.exec= execFunc
}
//a modification of setTimeout;
//this one takes _multiple_ functions _with_ arguments
//as an argument;
//you can use ID to set more than one timer
function setTimeoutG(whatFunctions, whatTime, ID){
if (ID == null){var Id= 0}
noSpace= whatFunctions.replace(/\s*;/g, “;”)
listOfFunctions= noSpace.split(“;”)
for (var i = 0; i < listOfFunctions.length; i++) {
test0= new String(listOfFunctions[i])
if (test0.charAt(test0.length-1) != ")"){
eval('' + "tmr"+Id + '= setTimeout(test0, whatTime)')
}else{
eval('' + 'timed'+Id+'Function'+i+'= new defun(' +"'"+ test0 + "'"+')')
eval('' + "tmr"+Id+ '=setTimeout("timed"+Id+"Function"+i+".exec()", whatTime)')
}
}
}
//this is sometimes usefull
function testFunction(what){
document.location.href= "javascript: void(" + what + ")"
}
[/sourcecode]
Celelalte posturi din seria asta: 1, 2.