javascript - 在圣诞节调用函数

标签 javascript

我在尝试只在圣诞节那天下“雪”时遇到了一些麻烦。我想出了如何捕获这一天,但我不确定如何让它正确调用 SnowStorm()。

var christmas = {
  month: 11,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  var isChristmas = (now.getMonth() == christmas.month && now.getDate() == christmas.date);

if (isChristmas)
    return ;
  else
    return ;
}

var snowStorm = null;

function SnowStorm() {
    (snowstorm code)

}

snowStorm = new SnowStorm();

最佳答案

如果你打算创建一个函数(而不是一个类),这里是版本:

var christmas = {
  month: 12,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  return (now.getMonth() == christmas.month && now.getDate() == christmas.date);
}

if (isItChristmas()){
    SnowStorm();
  }else{
    //not a christmas
}
    
function SnowStorm() {
    (snowstorm code)
}

如果您打算上课,请看这里:

var christmas = {
  month: 12,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  return (now.getMonth() == christmas.month && now.getDate() == christmas.date);
}

var storm = new SnowStorm();

if (isItChristmas()){
    storm.Snow();
  }else{
    //not a christmas
}
    
function SnowStorm() {
    this.Snow = function(){
        (snowstorm code)
    }
}

关于javascript - 在圣诞节调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8631915/

相关文章:

Javascript 搜索和替换?

javascript - 如何防止幻灯片循环回到开头?

javascript - 在 div 中添加嵌套的 if else 语句

javascript - Bootstrap Modal 缩小动画

javascript - jQuery 如何切换 ul 内的 li 位置

javascript - 确定变量是 ES2015 Map 还是普通的旧 JavaScript 对象

javascript - 使用 Rails,如何在应用程序的特定页面上执行 javascript 文件?

javascript - TypeScript - 为什么编译器允许具有指定显式类型的未定义类属性?

javascript - 如何按键获取Firebase数据库(Angular)中的值(value)?

javascript - 解释 "jest.advanceTimersToNextTimer "