Undersore的_.now如何工作? [英] How does Undersore's _.now work?

查看:81
本文介绍了Undersore的_.now如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来不是用JavaScript编写的。

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

如果在控制台中键入 _now ,则只会得到

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

_。now 用JavaScript引擎的本机代码做了什么?

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

推荐答案

默认为 _.now 只是 Date.now ,但在不支持它的环境中除外。在不支持 Date.now 的地方, _。now 将改用此实现(lodash也是一样)

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

由于您的浏览器支持 Date.now _。now 仅仅是本机实现的代理

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

注意:您还可以通过使用 Function.prototype.bind

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

这篇关于Undersore的_.now如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆