javascript - How does Undersore's _.now work? - Stack Overflow

It doesn't look like it is written in JavaScript. if you type _now in the console, you only getfun

It doesn't look like it is written in JavaScript.

if you type _now in the console, you only get

function now() { [native code] }

You usually only get that when you try to look at some built-in method where the inner-workings are invisible to the browser.

setTimeout
=>function setTimeout() { [native code] }

Has _.now done something with "native code" of the JavaScript engine?

It doesn't look like it is written in JavaScript.

if you type _now in the console, you only get

function now() { [native code] }

You usually only get that when you try to look at some built-in method where the inner-workings are invisible to the browser.

setTimeout
=>function setTimeout() { [native code] }

Has _.now done something with "native code" of the JavaScript engine?

Share Improve this question asked Jan 30, 2015 at 0:21 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges 1
  • 1 It probably just uses the native function if it exist. Otherwise it provides an implementation for it. – Alxandr Commented Jan 30, 2015 at 0:25
Add a ment  | 

3 Answers 3

Reset to default 7

By default _.now is just Date.now, except in environments that do not support it. Where Date.now isn't supported _.now will use this implementation instead (same goes for lodash)

_.now = function() {
   return (new Date()).getTime()
};

As your browser supports Date.now, _.now is just a proxy to the native implementation


Note: you can also make any of your functions appear as native in console by calling using Function.prototype.bind

function foo() {console.log('bar');}
var bar = foo.bind(null);

console.log(bar);
// => function () { [native code] }

Take a look at the underscore source code:

_.now = Date.now || function() {
  return new Date().getTime();
};

This means that it will use Date.now() if it exists, which is an internal function. Otherwise it will use new Date().getTime(), which is supported by all JavaScript engines.

It returns an integer timestamp for the current time. Useful for implementing timing/animation functions.

_.now(); => 1392066795351

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744939424a4602230.html

相关推荐

  • javascript - How does Undersore's _.now work? - Stack Overflow

    It doesn't look like it is written in JavaScript. if you type _now in the console, you only getfun

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信