Last active
November 16, 2015 17:15
-
-
Save crguezl/81f2bd2fd8f3c3510bb1 to your computer and use it in GitHub Desktop.
Constructor de la clase Respuesta para la práctica de SYTW en la que se pide ampliar el Modelo. Ejemplos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Respuesta(c) { | |
var f; | |
if ((typeof c === 'string') || (typeof c === 'number')) { | |
f = function(respuesta) { return respuesta === c; } | |
} | |
else if (c.constructor.name === 'RegExp') { | |
f = function(respuesta) { return c.exec(respuesta); } | |
} | |
else if (c.constructor.name === 'Function') { f = c; } | |
// else ... | |
f.__proto__ = this.__proto__; | |
return f; | |
} | |
var q = new Respuesta('hello'); | |
console.log(q instanceof Respuesta); //true | |
console.log(q('hello')); // true | |
console.log(q('pim')); // false | |
q = new Respuesta(/a+/); | |
console.log(q instanceof Respuesta); //true | |
console.log(q('haallo')); // [ 'aa', index: 1, input: 'haallo' ] que es true | |
console.log(q('bye')); // null que es false | |
q = new Respuesta(function(x) { return x === 'chuchu'}); | |
console.log(q instanceof Respuesta); //true | |
console.log(q('chuchu')); // true | |
console.log(q('bye')); // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Respuesta(c) { | |
if ((typeof c === 'string') || (typeof c === 'number')) { | |
return function(respuesta) { return respuesta === c; } | |
} | |
else if (c.constructor.name === 'RegExp') { | |
return function(respuesta) { return c.exec(respuesta); } | |
} | |
else if (c.constructor.name === 'Function') { return c; } | |
} | |
var q = Respuesta('hello'); | |
console.log(q('hello')); // true | |
console.log(q('pim')); // false | |
q = Respuesta(/a+/); | |
console.log(q('haallo')); // [ 'aa', index: 1, input: 'haallo' ] que es true | |
console.log(q('bye')); // null que es false | |
q = Respuesta(function(x) { return x === 'chuchu'}); | |
console.log(q('chuchu')); // true | |
console.log(q('bye')); // false |
- Práctica en SYTW 15/16 campusvirtual
- https://github.com/SYTW
- http://stackoverflow.com/questions/25586809/how-to-get-the-class-name-of-an-object-in-node-js
- http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class
- http://stackoverflow.com/questions/10341127/can-javascript-constructor-return-function-and-keep-inheritance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Este gist corresponde a la práctica Diseño Adaptativo, Despliegue en Heroku, Mejoras.
Ese es el enunciado:
Añada a la práctica anterior del Quiz:
Diseño adaptativo (véase CSS adaptable a móviles en YouTube)
Despliegue en Heroku (véase Despliegue en Heroku en YouTube)
Defina una clase o factoría
Respuesta
cuyo constructor/factoría admita como argumento:regexp
(el constructor/factoría lo convierte internamente a formato función así:function(x) { return x.match(regexp); }
)string
o un númeronumber
(el constructor/factoría lo convierte a formato función así:function(x) { return x === 'string'; }
o bienfunction(x) { return x === number; }
)Un objeto
Respuesta
es siempre una función que recibe un argumento con la respuesta escrita por el alumno y retornatrue
si y sólo si la respuesta es correcta.Añada una clase
Pregunta
de la que heredanPreguntaCorta
(que se representa en la vista mediante uninput
) yPreguntaLarga
(que se representará mediante unatextarea
).Reescriba el cuestionario con la nueva sintáxis