【问题标题】:Get only necessary constructor property within a called function在被调用函数中仅获取必要的构造函数属性
【发布时间】:2022-01-21 11:35:04
【问题描述】:

我对构造函数还很陌生。

出于个人目的,我正在制作一个扩展 Date() 对象可能性的脚本。

但是当我使用new 创建一个对象并请求一个特定的属性时,所有属性都会被处理(实际上我可以在控制台中看到)。如何避免这种情况以节省一些资源?

例如,如果我这样做:var bisextile = new RightTime(2022).IsBisextile,我不想计算 RightTime().NextFullMoon

谢谢

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
  }
}

【问题讨论】:

  • 在这种情况下,“节省资源”可以忽略不计。我们谈论的是几个字节的内存。但是,如果您担心将所有其他计算放在一个单独的函数中,并在需要时调用它们。您还可以class 一种更现代的代码方法。
  • 是的,我打算添加更多计算。并且日历将从这个函数中生成一些递归。当您说“单独的功能”时,您的意思是在 RightTime() 功能之外?还是没关系?感谢您对class 的建议。马上检查

标签: javascript constructor properties


【解决方案1】:

您可以使用getter

示例

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

或者使用现代class作为Andy推荐

示例

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

至于saving resources,我不知道您是如何得出结论的,但通常最好运行 js profiler 并查看潜在的罪魁祸首。

【讨论】:

  • 谢谢。除了 getter 不接受任何参数。并且类中的函数抛出:unexpected token: identifier。我需要一些属性的参数,例如:ShiftDays = (n) => { let d = new Date(this.Year,this.Month-1,this.Day); d.setDate(d.getDate() + n); return new RightTime(d.getFullYear(), d.getMonth()+1, d.getDate()) }
  • 是的,你不能在这种情况下使用 getter,ShiftDays 必须是例行程序。
  • 似乎没问题 ;) 使用 get 当我不调用它们时,我看不到其他参数的踪迹。谢谢!
  • 如果我想嵌套属性怎么办? Zodiac = () => { this.Emote = () => { return zodiacEmote[this.Month] } } console.log(new RightTime().Zodiac.Emote) 这不起作用。也不是父级中的另一个类,也不是getter
  • 更新了答案。不过,我会将有关十二生肖的信息保留在单独的班级中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多