9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

変数nowがいつでもどこでも現在時刻を持つようにする

Posted at
準備
Object.defineProperty(window, 'now', {
  get: function(){ return new Date(); }
});
使い方
console.log(now);

setTimeout(function() {
  console.log(now);
}, 1000);

ローカル変数との名前の重複については

var now = window.now;

で回避できるかと思います。

いや、これが現実的な使い方ではないとは思うんですが、getterて良いですね。うはー夢が広がりんぐ。

参考:

9
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?