Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why extend a function with an empty object?

Tags:

javascript

I was looking at the Rebound javascript code and saw that the SpringSystem function is being extended with an empty object.

var SpringSystem = rebound.SpringSystem = function SpringSystem() {
    this._springRegistry = {};
    this._activeSprings = [];
    this._listeners = [];
    this._idleSpringIndices = [];
    this._boundFrameCallback = bind(this._frameCallback, this);
};

extend(SpringSystem, {});

Where extend looks like:

function extend(target, source) {
    for (var key in source) {
        if (source.hasOwnProperty(key)) {
            target[key] = source[key];
        }
    }
}

What does the first extend achieve by extending an empty object? It's my understanding that an empty object has no hasOwnProperty(key) for anything.

What am I missing?

like image 699
Brad Avatar asked May 03 '26 17:05

Brad


1 Answers

You are missing nothing, the code serves no purpose. It looks like a leftover from a class template, notice that other classes and all prototypes use this pattern.

That's also why it has been removed in 2014.

like image 154
Bergi Avatar answered May 05 '26 06:05

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!