跟踪变量实例 [英] Keeping track of variable instances

查看:108
本文介绍了跟踪变量实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我创建一个名为 promiseRipple 的函数,该函数接受一个值为函数的对象字面值。每个函数可以包含同步代码或异步 promise

  var _ = require('lodash')
var Promise = require('bluebird')

函数promiseRipple(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
返回值
})
},null)
.then(function(){
return start
})
}

基本用法

  promiseRipple({zero:'zero'},{
'alpha ':function(data){
return Promise.resolve(data.zero +'alpha')// async - >'zero alpha'
},
'beta':function(数据){
return data.zero +'beta'// - >'zero beta'
},
'gamma':function(data){
return Promise。解决(data.zero +'gamma')// as ync - >'zero gamma'
},
'delta':function(data){
return Promise.resolve(data.zero + data.alpha +'delta')//异步 - > 'zerozero alpha delta'
},
})。then(function(results){
// results - > {
// zero:'zero',
// alpha:'zero alpha',
// beta:'zero beta',
// gamma:'zero gamma',
// delta:'zerozero alpha delta'
//}
})

我想添加一些功能make只要在函数中返回 data ,那么新的数据propeties将被扩展到现有的数据库中。

 promiseRipple({zero:'zero'},{
'alpha':function(data){
return Promise.resolve(data.zero +'alpha') // async - >'zero alpha'
},
'beta':function(data){
data.foo = data.zero +'foo'// - >'零foo'
data.bar = data.zero +'bar'// - >'zero bar'
返回数据
},
'gamma':function(data ){
返回Promise.resolve(data.zero +'gamma )// async - >'zero gamma'
},
'delta':function(data){
return Promise.resolve(data.zero + data.alpha +'delta' )//异步 - > '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 ')

函数开始(数据){
如果(数据)返回数据
返回{}
}
Start.prototype = Object.prototype

函数promiseRipple(start,props){
道具=(道具)?道具:开始
开始=(道具)?新开始(开始):新开始()
道具= _.mapValues(道具,功能(道具,钥匙){
道具钥匙=钥匙
返回道具
})
return Promise.reduce(_。values(props),function(result,action){
if(typeof action!=='function')throw new Error('property values must function')
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
返回值
}
})$ (b $ b},null)
.then(function(){
return start
})
}

module.exports = promiseRipple

什么是检测返回的对象是否与我们开始使用的对象相同的好方法,与对象的值?

解决方案

为了回答您的问题,您可以检查对象与返回值之间的等同性。



注意:

 函数f(obj){
obj.b ='b test';
return obj;
}

var obj_1 = {};
obj_1.a ='a test';
//这实际上只是对与obj_1
var obj_2 = f(obj_1)相同的对象的引用;

console.log(obj_1); // {a:a test,b:b test}
console.log(obj_2); // b:b test}
//以下是如何测试与在
中传递的原始对象是否相等的console.log(obj_1 === obj_2 ); // true


Below I create a function called promiseRipple takes in a object literal whose values are functions. Each function can contain synchronous code or an asynchronous 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
  })
}

Here's the basic usage

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' 
  // }
})

I wanted to add in some functionalty to make is so whenever data was returned within a function, the new data propeties would be extended to the existing.

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' 
  // }
})

I tried to make a custom object literal that would allow for me to detect if the returned value from the prop function was data or some new variable. However this does not work.

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

What is a good way to detect if the object returned is the same object that we started out with, without messing with the value of the object?

解决方案

As it's related to the motivation for your question, you should note that in JavaScript non-primitive variables are passed "by reference" to functions. This means that changes to the object within the function are going to be reflected in the object when it is referenced outside the function, and it doesn't matter if it is passed back.

To answer your question, you can just check for equality between the object and the return value.

Notice:

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

这篇关于跟踪变量实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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