小编mri*_*man的帖子

Javascript导出 - 实例化变量/对象

我正在尝试修改homebridge-wink3代码以添加变量,以便我可以跟踪状态.我家中有5个阴影,因此变量的每个实例都必须是唯一的.

shade.js文件中,它有;

exports.default = ({ Characteristic, Service }) => {
  return {
    type: "shade",
    group: "shades",
    services: [{
      service: Service.WindowCovering,
      characteristics: [{
        characteristic: Characteristic.TargetPosition,
        get: (state, desired_state) => desired_state.position * 100,
Run Code Online (Sandbox Code Playgroud)

我想更改get(和set代码中的其他地方),以便它使用局部变量lastState来跟踪状态.

    get: (state, desired_state) => { 
                if (desired_state.position != null) {
                        lastState = desired_state.position * 100;
                }
                else if (lastState != undefined) {
                        desired_state.position = lastState / 100;
                }
                return lastState;
Run Code Online (Sandbox Code Playgroud)

我花了几个小时试图找出如何让代码维护每个阴影(对象实例)的单个变量,但它们似乎总是共享lastState变量的相同实例.

我需要做什么?

有关代码,请参阅https://github.com/sibartlett/homebridge-wink3/blob/master/src/devices/shade.js.

javascript export

5
推荐指数
1
解决办法
170
查看次数

C结构的良好编码实践?

在C中,您可以定义结构来保存各种变量;

typedef struct {
    float       sp;
    float       K;                 // interactive form - for display only
    float       Ti;                //  values are based in seconds
    float       Td;
} pid_data_t;
Run Code Online (Sandbox Code Playgroud)

但让说K,TiTd不应该被公开设置,并且只能用于他们被操纵后存储的值.所以,我希望这些值不被更新;

pid_data_t = pid_data;
pid_data.K = 10;         // no good! changing K should be done via a function
Run Code Online (Sandbox Code Playgroud)

我想通过一个函数设置它们;

int8_t pid_set_pid_params(float new_K_dash, float new_Ti_dash, 
    float new_Td_dash)
{
    …                             // perform lots of things
    pid_data->K  = new_K_dash;
    pid_data->Ti = new_Ti_dash;
    pid_data->Td = new_Td_dash;
}
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?我知道C++使用类似于get/set属性,但是想知道人们可能在C上做什么.

c struct

3
推荐指数
1
解决办法
1997
查看次数

标签 统计

c ×1

export ×1

javascript ×1

struct ×1