Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

91
Vistas
Obtenga solo la propiedad de constructor necesaria dentro de una función llamada

Soy bastante nuevo en lo que respecta a las funciones de constructor.

Para fines personales, estoy creando un script que amplía las posibilidades del objeto Date().

Pero cuando creo un objeto con new y pido una propiedad específica, todas las propiedades se procesan (como puedo ver en la consola en realidad). ¿Cómo evitar eso para ahorrar algunos recursos?

Por ejemplo, si hago: var bisextile = new RightTime(2022).IsBisextile , no quiero que se RightTime().NextFullMoon .

Gracias

 const RightTime = function(a,m,j,h,min) { var getLunarAge = function(d,b,c) { d = void 0 === a ? new Date() : new Date(d,b,c); var b = d.getTime(); d = d.getTimezoneOffset(); b = (b / 86400000 - d / 1440 - 10962.6) / 29.530588853; b -= Math.floor(b); 0 > b && (b += 1); return 29.530588853 * b; }; var woy = function(yy,mm,dd) { var date = new Date(yy,mm-1,dd); date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); var week1 = new Date(date.getFullYear(), 0, 4); return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); }; a || m || j || h || min ? (!m && (m = 1), !j && (j = 1), !h && (h = 0), !min && (min = 0), this.Now = new Date(a, m - 1, j, h, min)) : this.Now = new Date() this.Day = this.Now.getDate(); this.DayInWeek = 0 == this.Now.getDay() ? 7 : this.Now.getDay(); this.DayInWeekName = days[this.DayInWeek]; this.Year = this.Now.getFullYear(); this.IsBissextile = ((this.Year % 4 === 0 && this.Year % 100 > 0) || (this.Year % 400 === 0)); this.Month = this.Now.getMonth() + 1; // (...) this.MoonAge = getLunarAge(this.Year, this.Month, this.Day); this.NextFullMoon = this.MoonAge > 14.765294427 ? 44.29588328 - this.MoonAge : 14.765294427 - this.MoonAge; this.NextNewMoon = 29.530588853 - this.MoonAge; var mn = Math.round((this.MoonAge * 8) / 29.530588853) // (...) this.ShiftDays = function(n) { let d = new Date(this.Year,this.Month-1,this.Day); d.setDate(d.getDate() + n); return d } }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede utilizar getter

Ejemplo

 const RightTime = function(a,m,j,h,min) { this.Now = new Date() this.Year = this.Now.getFullYear(); Object.assign(this, { get IsBissextile() { return (this.Year % 4 === 0 && this.Year % 100 > 0) || (this.Year % 400 === 0) } }) } const test = new RightTime(); console.log(test.IsBissextile);

O use la class moderna como recomendó Andy

Ejemplo

 class RightTime { constructor(a,m,j,h,min) { this.Now = new Date() this.Year = this.Now.getFullYear(); this.Month = this.Now.getMonth() + 1; } get IsBissextile() { return (this.Year % 4 === 0 && this.Year % 100 > 0) || (this.Year % 400 === 0) } get Zodiac() { const zodiacEmote = { 12: "♐", }; const month = this.Month; return { get Emote() { return zodiacEmote[month]; } } } } const test = new RightTime(); console.log(test.IsBissextile); console.log(test.Zodiac.Emote);

En cuanto a los saving resources , no sé cómo sacaste la conclusión, pero por lo general sería mejor ejecutar js profiler y ver los posibles culpables.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda