1

Years ago I used this technique because I did not know how to do it "right" with prototype. It's a technique I call Christmas tree OOD. You make a function which is like a constructor that makes a new object, attaches new properties to it, like a Christmas tree, and return the reference to it. Here is an example:

function RigidBody() {
    const newRB = document.createElement('canvas');
    
    newRB.dataMember = 5;
  
    newRB.memberFunction = function() {
        // do important rigid body stuff
    }

    return newRB;
}

Then you can use this function as a kind of base object to make other derived objects.

function Ship(locX, locY) {
    const newShip = new RigidBody();

    newShip.otherDataMember = 34;

    newShip.otherMemberFunction = function() {
        // some other important ship specific stuff to do
    }

    return newShip;
}

You can see that this simple thing I did can make N number of objects with a full "class" hierarchy of objects. Then you can just make an object of the derived object.

    const myShip = new Ship(100, 200);

I use class now and I don't plan on using this in the future but I'm curious to know if others have used this technique and if it has a name.

7
  • 1
    see: Factory Vs Prototype - What to use when? and Inheritance with factory method/class pattern which links to Learning JavaScript Design Patterns Commented Jun 3, 2022 at 21:21
  • They say class is only syntax sugar, it still uses function behind the scenes Commented Jun 3, 2022 at 21:22
  • Factory usually means returning potentially N number of different kinds of objects depending on a parameter. So it's kind of like factory. This technique definitely works but I would not use it for large programs. Commented Jun 3, 2022 at 23:10
  • 1/2 ... It is a composition technique based on factory-functions. Thus the OP's first mistake is the misuse of a factory as constructor. E.g. const newShip = new RigidBody(); is not necessary since newShip will never be an instanceof RigidBody. Despite calling the factory with the new operator, just a new canvas element is returned (as intended). No this binding is involved. Therefore the OP should rename RigidBody to createRigidBody which by name tells it's a factory function with no need for new. Commented Jun 5, 2022 at 12:57
  • 2/2 ... Quite a few people would classify this composition technique already as mixin-pattern. Some strong believers (non questioning disciples) worship objects created by this quite obvious technique as "Crockford Objects". Others refer to it as "Functional Inheritance" (coined by Crockford ) even though a better (not misleading) term could have been e.g. Function based Mixins since no inheritance magic is involved, and everything is straightforward object composition/aggregation. Commented Jun 5, 2022 at 12:57

1 Answer 1

1

It's still called OOP, since you're working with objects. Redefining member functions inside the constructor (when not necessary for this-binding) instead of putting them on the prototype is considered a bad practice, but doesn't change functionality or modelling.

Your technique for creating derived objects is known as parasitic inheritance, a term coined by Douglas Crockford afaict.

Sign up to request clarification or add additional context in comments.

1 Comment

After reading the links you included, especially about parasitic Inheritance I've decided that the way I did it is perfectly kosher. I did remove the "new"s though. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.