I find some of this info in other posts, but not a direct comparison. I'm curious as to the trades of speed and size efficiency, "public" (exposed) vs "private" (not exposed), and any other pros or cons of various ways of defining methods relative to Espruino. For example, consider the module...
// Foo.js: module methods examples...
function barNone() {
return 0;
};
function bar(n) {
return n;
};
function bar2(n) {
return n*n;
};
function Foo(){
this.bar = bar;
};
Foo.prototype.bar2 = bar2;
Foo.prototype.bar3 = function bar3(n) {
n = n || barNone();
return n*n*n;
};
exports = Foo
And the text case use ...
// FooBar.js: test of Foo.js module...
var foo = new (require("Foo"))();
console.log("foo",foo);
console.log("foo.bar(4)",foo.bar(4));
console.log("foo.bar2(4)",foo.bar2(4));
console.log("foo.bar3(4)",foo.bar3(4));
console.log("foo.bar3()",foo.bar3());
I'm sure one could come up with other method notions given the "rich" flexibility of JavaScript, but for starters I'd like to know which one of these declarations makes better sense and why.
The barNone declaration is not visible or usable outside the module, but the others are. The bar declaration is explicitly exposed to the user and makes for a more verbose listing of foo, but appears to minimize better. It would also seem to be faster as the object does not need to look down the prototype chain. However, this approach does not seem to be used. Is there some other downside, for example scope or code replication with additional "new" foo objects. And the prototype declarations are not explicitly visible, but still exposed to the user. What other reasons would you chose one way over another?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I find some of this info in other posts, but not a direct comparison. I'm curious as to the trades of speed and size efficiency, "public" (exposed) vs "private" (not exposed), and any other pros or cons of various ways of defining methods relative to Espruino. For example, consider the module...
And the text case use ...
Which produces the output...
I'm sure one could come up with other method notions given the "rich" flexibility of JavaScript, but for starters I'd like to know which one of these declarations makes better sense and why.
The barNone declaration is not visible or usable outside the module, but the others are. The bar declaration is explicitly exposed to the user and makes for a more verbose listing of foo, but appears to minimize better. It would also seem to be faster as the object does not need to look down the prototype chain. However, this approach does not seem to be used. Is there some other downside, for example scope or code replication with additional "new" foo objects. And the prototype declarations are not explicitly visible, but still exposed to the user. What other reasons would you chose one way over another?