Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created August 19, 2011 09:21
Show Gist options
  • Select an option

  • Save joseanpg/1156429 to your computer and use it in GitHub Desktop.

Select an option

Save joseanpg/1156429 to your computer and use it in GitHub Desktop.
Asignación por defecto mediante || (GEJS tercera reunión)
/*
* 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