我有一个域模型类型。它的众多属性之一需要 ITranslationService 来提供将其返回值转换为适当语言的能力。
我是否应该将 ITranslationService 注入域模型类型的构造函数中(因此必须在实例化类型的任何地方进行更改,并且在通过 NhIbernate 检索时必须关注初始化),即使它被类型的一小部分使用(一个许多属性);或者我可以使用另一种功能模式吗?
有没有人有相关的经验可以分享?
我目前缓存方法调用的结果。
缓存代码遵循标准模式:如果存在,则使用缓存中的项目;否则,计算结果,将其缓存以供将来调用,然后返回。
我想保护客户端代码免受高速缓存未命中(例如,项目过期时)。
我正在考虑生成一个线程来等待缓存对象的生命周期,然后在现有项过期时(或之前)运行提供的函数来重新填充缓存。
任何人都可以分享与此相关的任何经验吗?这听起来像是明智的做法吗?
我正在使用.NET 4.0。
鉴于:
function MyCtor() {}
var myInstance = new MyCtor(); //myInstance.constructor ==== MyCtor
var MyCtor = function() {}
var myInstance = new MyCtor(); //myInstance.constructor ==== Function
Run Code Online (Sandbox Code Playgroud)
如果您使用前一种模式实例化一个对象,则构造函数“更有意义”。
这些方法中的一种是首选吗?在某些情况下,一个人更惯用吗?
以下代码返回:
output.isPending?: true
output.isRejected?: false
output.isFulfilled?: false
Run Code Online (Sandbox Code Playgroud)
为什么?我原以为output.isRejected是true.
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.7/q.js"></script>
<script src="http://jasmine.github.io/2.3/lib/jasmine.js"></script>
</head>
<body>
</body>
<script>
var output, bar;
bar = {
doSomethingAsync: function() {
var d = Q.defer();
d.resolve('result');
return d.promise;
}
};
function Foo(bar) {
this._bar = bar;
this.go = function() {
var deferred = Q.defer();
this._bar.doSomethingAsync()
.then(onSuccess.bind(this, deferred));
return deferred.promise;
}
};
function onSuccess(deferred, result) {
deferred.reject();
}
output = new Foo(bar).go()
.finally(function() {
console.log('output.isPending?:', output.isPending());
console.log('output.isRejected?:', output.isRejected());
console.log('output.isFulfilled?:', output.isFulfilled());
}); …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我Mozilla是否已经改变了不实施HTML导入的立场(HTML5 Web Components规范的一部分)?
如果将setTimeout调用的回调添加到作业队列中(例如,如果它在作业队列中是下一个),clearTimeout则在事件循环的当前滴答中调用a ,以提供id原始setTimeout调用的。是否将setTimeout运行作业队列上的回调?
还是运行时神奇地从作业队列中删除了回调?
我看到一个测试在块jest.mock之外进行了调用describe。
我担心这会在测试之外造成可见的变化。我不希望这个测试影响其他测试。
应该jest.mock移动到描述块内还是按原样可以?
import target from '../';
jest.mock('../../../helpers/helpers.api', () => ({
httpPost(...args) {
return args;
}
}));
describe('my component', () => {
it('should foo', () => {
//...
});
});
Run Code Online (Sandbox Code Playgroud) 我想在传奇中引入延迟(使用redux-saga).
我怎样才能做到这一点?
如果redux-saga提供了API,我也会对如何手动实现它感兴趣.
function* save({ payload }) {
yield put(pending());
// I want to simply wait (non-blocking) here for say 2 seconds
yield put(complete());
}
Run Code Online (Sandbox Code Playgroud) 比较器函数ascending接受两个参数 - a和b.它必须返回一个比较两者的整数.
我有一个列表,我想按名称排序,所以我写了以下函数.
是否有一种功能习惯用法可以用来组合这两个功能,而不是byName负责编写结果函数?
const ascending = (a, b) => a.localeCompare(b);
const byName = (i) => i.get('name');
const useTogether = (...fns) => ...; // is there an idiomatic function like this?
// usage
items.sort(useTogether(byName(ascending)));
Run Code Online (Sandbox Code Playgroud) 为什么map, foreach, andreduce不使用迭代器函数 on Symbol.iterator?
class MyArray extends Array {
*[Symbol.iterator]() {
for(let x = 0; x < this.length; x++) { yield this[x]*2 }
}
}
const log = console.log
const arr = new MyArray(1,2,3)
console.log([...arr]) // [2,4,6]
log(arr.map((i) => i)) // [1,2,3]Run Code Online (Sandbox Code Playgroud)
和:
const arr = [1,2,3]
Object.defineProperty(Object.getPrototypeOf(arr), Symbol.iterator, {
value: function*() {
for(let x = 0; x < this.length; x++) { yield this[x]*2 }
}
})
const log = console.log
log([...arr]) // …Run Code Online (Sandbox Code Playgroud)javascript ×7
.net ×1
c# ×1
caching ×1
domain-model ×1
ecmascript-6 ×1
firefox ×1
jestjs ×1
q ×1
redux-saga ×1
settimeout ×1
testing ×1