05-20-2020 05:29 - edited 05-20-2020 05:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-20-2020 05:29 - edited 05-20-2020 05:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Answered! Go to the Best Answer.

- Labels:
-
JavaScript
Accepted Solutions
05-20-2020 21:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-20-2020 21:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

05-20-2020 13:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-20-2020 13:22
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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
}
})
Gondwana Software
05-20-2020 19:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-20-2020 19:31
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thank you. That helps. I will try it.

05-20-2020 21:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-20-2020 21:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

05-20-2020 21:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


05-20-2020 21:54
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

05-21-2020 05:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-21-2020 05:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
05-21-2020 11:27 - edited 05-21-2020 16:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-21-2020 11:27 - edited 05-21-2020 16:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

05-22-2020 21:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-22-2020 21:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
05-23-2020 08:17
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-23-2020 08:17
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

05-23-2020 10:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-23-2020 10:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
@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?
05-23-2020 19:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-23-2020 19:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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:
It's very thorough.
05-23-2020 20:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-23-2020 20:48
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

