Javascript Export - Activating Variables / Objects

I am trying to modify the homebridge-wink3 code to add a variable so that I can track the status. I have 5 shades in my house, so each instance of the variable must be unique.

In the shade.js file it has;

 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, 

I would like to change get (and set elsewhere in the code) so that it uses the local lastState variable to track state.

  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; 

I spent several hours trying to figure out how to maintain the code in separate shadows (an instance of an object), but they always seem to use the same instance of the lastState variable.

What do i need to do here?

See https://github.com/sibartlett/homebridge-wink3/blob/master/src/devices/shade.js for code.

+5
source share
2 answers

Important: I understand that you want to clone an object ( lastState or an object using the get and set method).

Suppose I have an object A, like this:

 var A = { aVariable: "Panem et circencem", aMethod: function () { return (["Veni", "vidi", "vici"]); } }; 

Now suppose I want to clone an object A to an object B.

 function clone(obj) { if (null == obj || "object" != typeof obj) return obj; var copy = obj.constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; } return copy; } var B = clone(A); 

This is an example:

 var A = { aVariable: "Panem et circencem", aMethod: function () { return (["Veni", "vidi", "vici"]); } }; function clone(obj) { if (null == obj || "object" != typeof obj) return obj; var copy = obj.constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; } return copy; } var B = clone(A); B.aVariable = "Ad gloriam"; console.log(B); console.log (A); 

Then you can clone / copy your entire object to have some difference properties in your objects or clone lastState in your code. Sorry, I do not understand this part of your question.

Note: this question will try to answer the question. If I do not understand the question, please give me a comment.

Also note: If I do not answer the question, you can use the above inscription and copy my post to answer the question.

Also note: If you have a question, tell me a comment.

+1
source

You can declare lastState just above the return statement,

 let lastState; return { type: "shade", group: "shades", 

or higher export instructions,

 let lastState; export default ({ Characteristic, Service }) => { 

if you declare lastState in the same area where you create 5 instances, then all of them will have the same lastState .

+1
source

Source: https://habr.com/ru/post/1272977/


All Articles