codecamp

Lodash _.runInContext

_.runInContext([context=root]) 

创建一个给定context上下文对象的原始的 lodash 函数。

添加版本

1.1.0

参数

  1. [context=root] (Object): 上下文对象。

返回

(Function): 返回新的 lodash 对象

例子

_.mixin({ 'foo': _.constant('foo') });
 var lodash = _.runInContext();lodash.mixin({ 'bar': lodash.constant('bar') });
 _.isFunction(_.foo);
// => true
_.isFunction(_.bar);
// => false
 lodash.isFunction(lodash.foo);
// => false
lodash.isFunction(lodash.bar);
// => true
 // 使用 `context` 模拟 `Date#getTime` 调用 `_.now`
var stubbed = _.runInContext({  'Date': function() {    return { 'getTime': stubGetTime };  }}); 
// 或者在 Node.js 中创建一个更高级的 `defer`
var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;


Lodash _.rangeRight
Lodash _.stubArray
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Lodash 简介

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }