Ver Mensaje Individual
  #8 (permalink)  
Antiguo 30/05/2014, 16:44
Avatar de Alexis88
Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 6 meses
Puntos: 977
Respuesta: [APORTE] Función range() equivalente a su similar de PHP

Para crear métodos estáticos, no es necesario el uso de prototype, basta con hacer uso de la Dot Notation o la Bracket Notation para crear métodos estáticos en la función, pues al fin y al cabo, en JS todos son objetos.

Lo malo está en que al ser estático, al llamar a la función para generar otro array, se añade al que previamente se creó. Veré si puedo adaptar esto.

Edito: Sí se pudo.

Código Javascript:
Ver original
  1. var range = function(start, end, step){
  2.         range.array = range.array || [];
  3.         if (typeof start === typeof end){
  4.             range.array.push(typeof start === "string" ? start[0] : start);
  5.             step = start > end ?
  6.                    !isNaN(step) && isFinite(step) && step < 0 ? step : -step || -1 :
  7.                    !isNaN(step) && isFinite(step) ? step : 1;
  8.             start = typeof start === "string" ?
  9.                     String.fromCharCode(start.charCodeAt(0) + step) :
  10.                     start += step;
  11.             return (step > 0 && start <= end) || (step < 0 && start >= end) ?
  12.                     range(start, end, step) : (function(){
  13.                         var aux = range.array;
  14.                         range.array = [];
  15.                         return aux;
  16.                     })();
  17.         }
  18.         return false;
  19.     };
  20.  
  21. console.log(range(1, 10)); //1,2,3,4,5,6,7,8,9,10
  22. console.log(range("a", "z")); //a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand