Cateva functii in JavaScript. Le-am construit incercand sa realizez un interpretor JavaScript scris tot in JavaScript. In principiu, la sfarsit ar trebui sa poti introduce un programas scris in JavaScript intr-un formular dintr-o pagina HTML, sa dai click pe un buton si sa poti rula / testa programul respectiv. Daca vrea cineva sa se amuze,…
//defining universal variables inside a function (example: 'testVar'); I don't
//know what would it 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 usefull
function testFunction(what){
document.location.href= "javascript: void(" + what + ")"
}
De ce nu poti sa eval() tot continutul campului?
!? 🙂 despre care eval() vb?
Nu mi-a iesit. Ceva nu e in regula cu codul