IMNSHO, using object oriented (oo) terms is not a matter of code snippets... it's a matter of communicating apples and oranges... less talking about comparing them.
To bad that it is Python, but replacing self with this, _init_ with constructor and leaving out def, it is pretty close to JavaScript... after all, Python uses a lot (of the same as or) from JavaScript.
Now we do some changes to any code and discuss it...
The shortest way though is sneak peek into the - or one of the best practice - solutions, like peeking at the solution for the Sudoku challenges in an airline magazine.
Here the solutions, clear and straight, with all bells and whistles, and naming ad casing as best practices suggest (single saves and run... bulls-eye):
// Application (project) using StopWatch class going as
// stopWatchApp.js file into projects folder.
var StopWatch = require('StopWatch'); // get class
var stopWatch1 = new StopWatch(); // create instance
function onInit(){
stopWatch1.start();
setTimeout( function(){
stopWatch1.stop();
console.log('stopWatch1: duration in sec: ',
( stopWatch1.duration() / 1000 ).toFixed(2) );
}, 1000);
var stopWatch2 = new StopWatch("autostart");
setTimeout( function(){
stopWatch2.stop();
console.log('stopWatch2: duration in sec: ',
( stopWatch2.duration() / 1000 ).toFixed(2) );
}, 2000);
var stopWatch3 = new StopWatch("autostart");
try {
stopWatch3.start();
} catch(x) {
console.log('stopWatch3: start() exception: ',x);
}
try {
console.log(stopWatch3.duration());
} catch(x) {
console.log('stopWatch3: duration() exception: ',x);
}
}
setTimeout(onInit,1000);
Output attached as screenshot.
Notice the time sequence... StopWatch instance 3 gets bothered before instance 1 and 2 stop and show the duration.
Instead of getting the class (line 3) and re-use it with all the new, one could require the class for every new (with not much performance penalty): ... = new (require("StopWatch"))() and ... = new (require("StopWatch"))("autostart"). Instead of throwing simple text exception, Error or RYO Exception class or object in literal notation or JSON could be used.
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.
IMNSHO, using object oriented (oo) terms is not a matter of code snippets... it's a matter of communicating apples and oranges... less talking about comparing them.
A good introductory start is http://www.academia.edu/download/32169601/Object-Oriented_Software_Construction.pdf - explains the concepts and the various language bindings.
1400+ page is a very looooong start. So let's get a shorter one - 2 clips:
To bad that it is Python, but replacing
self
withthis
,_init_
withconstructor
and leaving outdef
, it is pretty close to JavaScript... after all, Python uses a lot (of the same as or) from JavaScript.And there is MDN with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects - at times confusing, because it shows all the options of oo pre-ES6 and ES6+ (and the ES6 modules aren't there yet... but the node.js).
Now we do some changes to any code and discuss it...
The shortest way though is sneak peek into the - or one of the best practice - solutions, like peeking at the solution for the Sudoku challenges in an airline magazine.
Here the solutions, clear and straight, with all bells and whistles, and naming ad casing as best practices suggest (single saves and run... bulls-eye):
Output attached as screenshot.
Notice the time sequence... StopWatch instance 3 gets bothered before instance 1 and 2 stop and show the duration.
Instead of getting the class (line 3) and re-use it with all the
new
, one could require the class for every new (with not much performance penalty):... = new (require("StopWatch"))()
and... = new (require("StopWatch"))("autostart")
. Instead of throwing simple text exception, Error or RYO Exception class or object in literal notation or JSON could be used.1 Attachment