Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for "Abstract" functions in JavaScript?

Tags:

I just wrote some JavaScript code that follows along with what I believe to be good practice for creating an object with closure and some functions:

var myStuff = (function() {
 var number = 0;
 var fn = {};
 fn.increment = function() { number++; };
 fn.decrement = function() { number--; };
 fn.getNumber = function() { return number; };
 return fn;
})();

myStuff.increment();
myStuff.increment();
alert(myStuff.getNumber()); // alerts '2'

I have no problem writing code like the previous snippet. I would like to write some code with functionality similar to a OOP "abstract" class. Here is the result of my effort:

var myStuff = (function () {
var number = 0;
var fn = {};
fn.increment = function () { number++; };
fn.decrement = function () { number--; };
fn.doSomethingCrazy = function () { throw new Error('not implemented'); }; // I want to specify later what this does.
fn.doSomethingCrazyTwice = function () { fn.doSomethingCrazy(); fn.doSomethingCrazy(); };
fn.getNumber = function () { return number; };
return fn;
})();

myStuff.doSomethingCrazy = function () { this.increment(); this.increment(); };
myStuff.doSomethingCrazyTwice();
alert(myStuff.getNumber()); // alerts '4'

The above code snippet works, but it doesn't seem graceful. Perhaps I'm trying to force JavaScript (a functional language) to do something it isn't designed to do (object inheritance)

What is a good way to define an object in JavaScript so that a function of that object can be defined later?

like image 581
Vivian River Avatar asked Sep 19 '11 21:09

Vivian River


2 Answers

Just don't define the function.

Javascript is a duck-typed language. If it looks like a duck and it quacks like a duck, it is a duck.
You don't need to do anything special to make this work; as long as the function exists when you call it, it will work fine.

If you call it on an instance that doesn't have the function, you'll get an error at the callsite.

like image 127
SLaks Avatar answered Sep 30 '22 17:09

SLaks


I agree with SLaks, there's no need to define the function, but I tend to anyway. That's because to me the important part is in the documentation. When somebody reads my class, I want it to be clear that you must implement these methods, what arguments will be passed and what should be returned.

This is from a file at work. There were multiple implementations of a feature with a base class that did the data loading at intervals.

/**
 * Called when data is received and should update the data buffer
 * for each of the charts 
 * 
 * @abstract
 * @param {cci.ads.Wave[]} waves
 * @void
 */
updateChartsData: function(waves){
    throw "Abstract method updateChartsData not implemented";
},

2019 Update

Use TypeScript if you can Declaring abstract method in TypeScript

like image 30
Juan Mendes Avatar answered Sep 30 '22 17:09

Juan Mendes