Created
August 19, 2011 09:21
-
-
Save joseanpg/1156429 to your computer and use it in GitHub Desktop.
Asignación por defecto mediante || (GEJS tercera reunión)
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
| /* | |
| * El siguiente patrón | |
| * | |
| * function foo(param) { | |
| * param = param || valor_por_defecto | |
| * ... | |
| * | |
| * no representa ningún riesgo de cara al rendimiento. | |
| * | |
| * Con ECMAScript 5 tenemos la posibilidad | |
| * de definir Named Accessor Properties, | |
| * propiedades en las que una asignación | |
| * es "more than meets the eye". | |
| * | |
| * En consecuencia si puede presentar "problemas" de velocidad | |
| * la siguiente construcción | |
| * | |
| * function foo(obj) { | |
| * obj.prop_with_set = obj.prop_with_set || valor_por_defecto | |
| * | |
| * Aún así sólo corremos ese riesgo cuando el valor de la propiedad | |
| * pasado por ToBoolean de false | |
| * | |
| * | |
| */ | |
| function factorial(n) { | |
| return n===0 ? 1 : n*factorial(n-1); | |
| } | |
| function makeDangerZone() { | |
| var privateValue = null; | |
| return { | |
| get value() { | |
| return 'I am hiding this value:'+valprivateValue; | |
| }, | |
| set value(boom) { | |
| console.log('Do you thing this is free? Wrong!!'); | |
| privateValue =factorial(10*boom); } | |
| } | |
| } | |
| function thatIsF5noMIG(turbofan) { | |
| var turbofan.value = turbofan.value || 1; | |
| console.log(turbofan.value); | |
| } | |
| var tomcatF14 = makeDangerZone(); | |
| tomcatF14.value = 10; //Factorial de 100 | |
| thatIsF5noMIG(tomcatF14);//Otra vez el factorial de 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment