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 |
Author
crguezl
commented
Nov 16, 2015
- 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