Javascript POO window[fnstring](param)
Bonjour,
J'essaye d'appliquer le paradigme POO à mon code javascript. Mais je bloque sur un message d'erreur
Uncaught TypeError: window[fnstring] is not a function
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
class Test {
// FONCTION QUI CONVERTIT UN STRING EN FONCTION
stringToFunction(rule, idField) {
var fnstring = rule; //string
var param = idField; /: objet
var fn = window[fnstring](param); // this ? lorsque je sors tout mon code de la classe, cela fonctionne. Dans la class = window[fnstring] is not a function ?
if (typeof fn === "function") {
return fn();
} else {
console.log(typeof(fn));
}
}
// FONCTION QUI BOUCLES SUR LES REGLES
controls (idField, action, rules) {
var that = this;
idField.addEventListener(action, function() {
for (var i = 0 ; i <rules.length; i++) {
let rule = rules[i];
let error;
that.stringToFunction(rule, idField);
...
}
});
}
} // FIN CLASS
nom = document.getElementById('nom');
var test1 = new Test();
test1.controls(nom, 'focusout', ['test1', 'test2']; |
Merci pour vos explications et vos conseils accessibles pour un autodidacte....