It is a pure-Javascript method returning a simple object,
MyClass.retObj = function() {
return {
x: {a:1, b:this.b}
,y: this.y
};
}
document.addEventListener('DOMContentLoaded', function(){ // ONLOAD:
console.log( MyClass.retObj().x ) // good...
console.log( MyClass.retObj() ) // BAD! show __proto__
}, false); // \ONLOAD
Why it returns a __proto__
in a modern browser? How to disable this ugly behaviour in nowadays (2019)?
MDN says that this feature "is no longer recommended".
PS: it is not a duplicate of this question, because here is the simplest and direct context, and I need a simple answer... And it is about "nowadays".
Why [does it show] a
__proto__
?
The prototype chain is very important if you want to understand why an object behaves in a certain way. Wether it is displayed as __proto__
or [[Prototype]]
or <prototype>
is not really relevant, is it? ...
How to disable this ugly [object visualization]?
Use another browser if that really bothers you.
curiously MyClass.retObj().x [does not seem to have]
__proto__
.
Thats indeed fun, probably the console thinks that the Prototype is not relevant to the audience, therefore it hides it, however it isn't really in both cases. On the other hand however this dynamic behaviour of visualizing things is kind of missleading, as one might think that __proto__
does not exist on one object but the other (it does exist on both).
MDN says that [
__proto__
] "is no longer recommended". How to disable this ugly [__proto__
property]?
And I wouldn't recommend using var
, backwards compability is ugly but mandatory. Removing all those bad design decisions would "break the internet", therefore keeping them is definetly better.
But are there no problem[s]?
Yes, __proto__
is problematic to performance optimizations, but ... it's JavaScript, the browsers know how to optimize the uncertain.
returning an object that is not what I defined?
You also haven't defined {}.toString()
. If you really need a completely blank object, use
Object.create(null)
forcing the ingestion of so much rubbish for users of my method?
__proto__
is not enumerable and inherited, you won't find it by accident programmatically if you are not searching for it actively.
No clean JSON?
JSON.stringify({}) // {} ?!?