Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setters in the SDK

ANSWERED

Hello,

I am sorry to ask about such a NOOBish topic, but I have been struggling with ]setters in the SDK.  I am making a grade calculator, and I have no problem with imports, and the actual pre-made calculator is working well.  However, I want to allow users to change the grade range and it is not working.  It seems the get and set keywords do not work (I get the error "expected a comma after set"), and if I put it in a class, it seems the compiler cannot read export functions.

 

I am fully aware that this is my own ignorance of JavaScript, however, none of the solutions I have found for node.js or regular JavaScript seem to work for me.

 

Here is what I have currently.  This is not the actual app, I made a new one just to test getters and setters.  This is the full code for that.

 

in index.js...

 

/*
 * Entry point for the watch app
 */
import * as calculation from "../common/Calculation.js"

console.log("App code started");

calculation.Calculator.calculate(3)

 

In my calculation.js

 

class Calculator{

export function calculate(i) {
  newNumber = 0;
        var newNumber = 5+i;
  this.newNumber = newNumber;
}
/*function getNumber(){
  return newNumber;
}
var newNumber = getNumber();
*/
console.log(`${newNumber}`);
  
}

let cal = new Calculator();

 

 Thanks for reading.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Not really. There are heaps of different ways to do classes/objects in JS (even in ES5.1). You could try getting a setter to work in the same file as the calling code (ie, don't try import/export initially). Once you've got that going, refactor.

 

Here's a little something that probably won't help. There are probably better ways to do it.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
0 Votes
11 REPLIES 11

Setters can work. You just have to be careful to use ES5.1 dialect on the watch.

 

Some examples I've used:

set log(val) {_log = val; _maxValue = 0}
Object.defineProperty(Gauge.prototype, 'units', {
  set: function set(val) {
    this._unitsEl.text = val
  }
})
Peter McLennan
Gondwana Software
Best Answer

Thank you.  That helps.  I will try it.

 

 

Best Answer
0 Votes

So, I tried for a while, and no matter what I did, I cannot get either of those to work with an export function that is called from index.js do you have any hints?

Best Answer
0 Votes

Not really. There are heaps of different ways to do classes/objects in JS (even in ES5.1). You could try getting a setter to work in the same file as the calling code (ie, don't try import/export initially). Once you've got that going, refactor.

 

Here's a little something that probably won't help. There are probably better ways to do it.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Actually,

That is exactly what I was looking for.  My confusion was how to  utilize the objects between files. I could get it working in one document, but when trying to move the object between, it would fail.  This framework helps and models the way I was trying to utilize.  Thank you.  It will take me a bit, but I will share the solution (based on your model) when I am done.

Best Answer

It works.  Here is the code I am using.

In index.js

 

 

/*
 * Entry point for the watch app
 */
import {Calculator} from "../common/Calculation.js"

console.log("App code started");

//Construct the calculator
const myCalculator = new Calculator('myCalculator')
//Set the variable value
myCalculator.calculate = 20

 

 

In Calculation.js

 

 

//create the export function
export function Calculator(_input) {
  //pass the value
  this._input = _input
}
//create the setter
Object.defineProperty(Calculator.prototype, 'calculate', {
  set: function set(val) {
    if (this._calculate === val) return
    this._calculate = val
    this._calculation()
  }
})
//Do something with the data.
Calculator.prototype._calculation = function() {
  let input = this._calculate;
  let output = 5*input
console.log(`The raw number is... ${output},`) 
}

 

 

 

My errors before were plentiful, but the biggest were...

  • You cannot import the file with `import * as ...` and expect it to work.  You need to specify the class.
  • You cannot access the imported values directly.  You need to use the power of OOP (of which, javascript handles, quite well) to access them.

Thank you again for all of your help.

Best Answer
0 Votes

Apologies for the kibitz...

 

Objects work, functions as variables work, modular programming works. They really did make this very complete, and I do like good OOP.

 

That said...

 

We're programming inside a very small box, objects are huge, and passing variables by reference in javascript is hinky.

 

If you intend to do anything complex, a single monolithic file with simple functions and variables takes a lot less memory.

Best Answer

Thank you for the info.  It is helpful.  I am new to javascript, and I am certainly feeling its hinkiness.  As far as system resources go, is there a way to see, or any hints as to where to look, to find the resources your application is using?  I love how the Arduino IDE says "Your app is X size and uses Y amount of memory."  Is there anything similar?

Best Answer
0 Votes

@LekoFraggle wrote:

Thank you for the info.  It is helpful.  I am new to javascript, and I am certainly feeling its hinkiness.  As far as system resources go, is there a way to see, or any hints as to where to look, to find the resources your application is using?  I love how the Arduino IDE says "Your app is X size and uses Y amount of memory."  Is there anything similar?


Try the following but keep in mind that Fitbit OS reclaims memory really well,... until it can't.
 
import {memoryfrom "system"
 
console.log("Mem",memory.js.used+"/"+memory.js.total+" Peak:"+memory.js.peak+" Pressure:"+memory.monitor.pressure)
 
When you start getting jerryscript errors, it's time to start optimizing.
 
Best Answer

Arduino is extremely cool. You can make almost anything (electronic) with an Arduino Nano.

 

Hey, if you want a good tutorial on javascript you could try the W3 school:

 

https://www.w3schools.com/js/ 

 

It's very thorough.

Best Answer

Thank you for your help.  I will look into Wc3.  My confusion is the different versions of javascript.  I think going through their tutorials from beginning to end may help.  Yes, I have a few Arduino Nanos, and they are incredible.  Your comments are helping significantly. My app is close to finished 🙂 with no memory problems currently.

Best Answer
0 Votes