简体   繁体   English

存储对具有原始值的变量的引用

[英]Store reference to a variable with a primitive value

I would like to write a function with a signature like 我想写一个带有签名的函数

broadcast(centerPiece.rotation.y);

which stores the passed variable and publishes the value at the variable on all animation frames there after. 它存储了传递的变量,并在此后的所有动画帧上的变量处发布值。

The issue is that I'm mostly interested in broadcasting primitive values, meaning the function above grabs the primitive value at the moment of the function call and keeps broadcasting that fixed value. 问题是我对广播原始值最感兴趣,这意味着上面的函数在函数调用时获取原始值并保持广播该固定值。

Currently I'm using this workaround 目前,我正在使用此替代方法

broadcast = function(obj,prop){
  doBusinessLogicWith(obj[prop])
}

giving me a rather ugly signature in my code looking like 在我的代码中给我一个相当丑陋的签名,看起来像

broadcast(centerPiece.rotation,'y');

This currently works because universally I will only need to broadcast properties on objects, but ... it's a little ugly. 当前这是可行的,因为一般而言,我只需要广播对象的属性,但是...有点难看。 Do I have better options for tracking a variable storying a primitive value? 我是否有更好的选择来跟踪讲述原始值的变量?

One slightly less ugly option might be to pass in a reference to a function that gets the current value. 较不那么丑陋的选择可能是传递对获取当前值的函数的引用。 Something like: 就像是:

broadcast = function(getValue){
  var curVal = getValue();
  doBusinessLogicWith(curVal);
}

Then call it with: 然后调用:

broadcast(function ()
{
   return centerPiece.rotation.y;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM