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

96
Vistas
Get only necessary constructor property within a called function

I'm quite new concerning constructor functions.

For personal purposes, I'm making a script that extends the possibilities of the Date() object.

But when I create an object with new and asking for a specific property, all properties are processed (as I can see in the console actually). How to avoid that to save some resources ?

For example, if I do : var bisextile = new RightTime(2022).IsBisextile, I don't want RightTime().NextFullMoon being calculated.

Thanks

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

You might utilize getter

Example

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);

Or use modern class as Andy recommended

Example

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);

As for the saving resources, I don't know how did you draw the conclusion but usually it would be better to run js profiler and see there potential culprits.

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