4 ms·
I dislike classes but I like bundling data along with methods which operate on that data. I often find that JavaScript's classless objects are a great solution
by daviddaviddavid 15y ago
I dislike classes but I like bundling data along with methods which operate on that data. I often find that JavaScript's classless objects are a great solution. It's just so natural to be able to create an object with object literal syntax without first creating an ad hoc class and then instantiating it:
var o = {
name: 'David',
greet: function () {
console.log('Hi, I am ' + this.name);
}
};
- njharman 15y ago> bundling data along with methods which operate on that data What can you possibly believe a class is other than exactly that?
- haldean 15y agoPolymorphism and type hierarchies.
- daviddaviddavid 15y agoI realize that is one thing that classes offer. My point is that you can achieve exactly the same thing with objects rather than classes. Very often when writing Python (or whatever) you'll create a class which is only ever instantiated once and the instantiation is done simply for the benefit of calling one method. In such cases I find classes to be overkill. What I really want is the object. I don't need to factor the object's behavior out into a class. Many of my sympathies are expressed much more thoroughly and eloquently in the many papers you can find online by Walter Smith outlining the motivations behind NewtonScript.
- jmaygarden 15y agoWouldn't this be better? function greet(name) { console.log('Hi, I am ' + name); } var o = function() { greet('David'); };