EDIT: This is NOT a duplicate.
While the answer is great, the question applies to the context of loopbackjs. There, most properties are accessed asynchronously. I tried the proposed solution and it doesn't work because the property count
is accessed asynchronously:
node.__count__children(function(err, count) {
if (err) {
console.log(err);
return;
}
return count;
});
This means I can't rely on the language features directly, but must account for asynchronicity. EDIT END
In django/python, I could define a property like so:
@property
def count_children(self):
return len(self.children)
Is this possible in javascript as well? Actually I need it in the context of loopback, but maybe it's a feature of the language?
I have tried manipulating the prototype:
Node.prototype.count_children = function() {
return this.children.length;
}
But this just adds a function count_children() to the object, it doesn't put the value of the function as property. Is that possible in javascript?