[英]Keeping track of variable instances
下面我創建一個名為promiseRipple
的函數, promiseRipple
接受一個對象文字,其值是函數。 每個函數都可以包含同步代碼或異步promise
。
var _ = require('lodash')
var Promise = require('bluebird')
function promiseRipple (start, props) {
props = (props) ? props : start
start = (props) ? start : {}
props = _.mapValues(props, function (prop, key) {
prop.key = key
return prop
})
return Promise.reduce(_.values(props), function (result, action) {
if (typeof action !== 'function') throw new Error('property values must be functions')
return Promise.resolve(action(start)).then(function (value) {
start[action.key] = value
return value
})
}, null)
.then(function () {
return start
})
}
這是基本用法
promiseRipple({zero: 'zero'}, {
'alpha': function (data) {
return Promise.resolve(data.zero + ' alpha') // async -> 'zero alpha'
},
'beta': function (data) {
return data.zero + ' beta' // -> 'zero beta'
},
'gamma': function (data) {
return Promise.resolve(data.zero + ' gamma') // async -> 'zero gamma'
},
'delta': function (data) {
return Promise.resolve(data.zero + data.alpha + ' delta') // async -> 'zerozero alpha delta'
},
}).then(function (results){
// results -> {
// zero: 'zero',
// alpha: 'zero alpha',
// beta: 'zero beta',
// gamma: 'zero gamma',
// delta: 'zerozero alpha delta'
// }
})
我想添加一些功能,以便在函數內返回data
時,新的數據屬性將擴展到現有的。
promiseRipple({zero: 'zero'}, {
'alpha': function (data) {
return Promise.resolve(data.zero + ' alpha') // async -> 'zero alpha'
},
'beta': function (data) {
data.foo = data.zero + ' foo' // -> 'zero foo'
data.bar = data.zero + ' bar' // -> 'zero bar'
return data
},
'gamma': function (data) {
return Promise.resolve(data.zero + ' gamma') // async -> 'zero gamma'
},
'delta': function (data) {
return Promise.resolve(data.zero + data.alpha + ' delta') // async -> 'zerozero alpha delta'
},
}).then(function (results){
// results -> {
// zero: 'zero',
// alpha: 'zero alpha',
// foo: 'zero foo',
// bar: 'zero bar',
// gamma: 'zero gamma',
// delta: 'zerozero alpha delta'
// }
})
我試圖創建一個自定義對象文字,允許我檢測prop函數返回的值是data
還是一些新變量。 但是這不起作用。
var _ = require('lodash')
var Promise = require('bluebird')
function Start (data) {
if (data) return data
return {}
}
Start.prototype = Object.prototype
function promiseRipple (start, props) {
props = (props) ? props : start
start = (props) ? new Start(start) : new Start()
props = _.mapValues(props, function (prop, key) {
prop.key = key
return prop
})
return Promise.reduce(_.values(props), function (result, action) {
if (typeof action !== 'function') throw new Error('property values must be functions')
return Promise.resolve(action(start)).then(function (value) {
console.log(value instanceof Start)
if (value instanceof Start) {
_.extend(start, value)
return start
} else {
start[action.key] = value
return value
}
})
}, null)
.then(function () {
return start
})
}
module.exports = promiseRipple
有什么好方法可以檢測返回的對象是否與我們開始時的對象相同,而不會弄亂對象的值?
由於它與您的問題的動機有關,您應該注意到,在JavaScript中,非原始變量通過“引用”傳遞給函數。 這意味着函數內對象的更改將在函數外部引用時反映在對象中,如果它被傳回則無關緊要。
要回答您的問題,您可以檢查對象和返回值之間的相等性。
注意:
function f(obj) {
obj.b = 'b test';
return obj;
}
var obj_1 = {};
obj_1.a = 'a test';
// This is really just a reference to the same object as obj_1
var obj_2 = f(obj_1);
console.log(obj_1); // {a: "a test", b: "b test"}
console.log(obj_2); // {a: "a test", b: "b test"}
// Here is how you can test for equality with the original object passed in
console.log(obj_1 === obj_2); // true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.