GLTFLoader bug when add method to Object.prototype

GLTFLoader error if in any file of project add method to Object.prototype

Example https://codepen.io/raidan00/pen/LYoxwbN

In console -TypeError: Cannot read properties of undefined (reading ‘bufferView’) at GLTFParser.loadAccessor

Is it possible to fix this? Or is it better to create a issue on GitHub?

Messing up with Object.prototype is like changing a fundamental physics constant in Nature. It may stay unnoticed for some time, but most likely whole universe will collapse.

Instead of:

Object.prototype.itWillCreateBug = function(...){...}

consider using:

Object.defineProperty(Object.prototype, 'itWillCreateBug', {
    value: function(...) {...}
});
3 Likes

Thank you. It helps.

2 Likes

Also extending prototypes of things like Object or Array can cause the entire JS stack to not optimize those basic classes anymore for your app, causing a drastic decrease in performance. Be wary of “helper” libraries that extend object/array.

I learned this the really hard way.

I understand this is not best solution to add method to Object.prototype. But according to JavaScript specification this is available. GLTFLoader do not working when use some JavaScript feature. I think this is bug.

I’d be happy to review pull requests adding the appropriate hasOwnProperty checks in GLTFLoader, or similar solutions. That said — I fully agree with @manthrax that extending prototypes of built-in objects will lead to more pain than just this bug, and is better avoided.