Is it not well to extend Object.prototype derictly?

J

jidixuelang

As I know,it's not well to extend Object.prototype derictly.
In the Prototype(JS Framewoke),there is no extend Object.prototype.
It only add some static method for Object class.
I want to konw the reason.Who can give me some advise!?
 
M

Martin Honnen

As I know,it's not well to extend Object.prototype derictly.
In the Prototype(JS Framewoke),there is no extend Object.prototype.
It only add some static method for Object class.
I want to konw the reason.Who can give me some advise!?

for..in enumeration will enumerate the properties added to
Object.prototype, try e.g.

Object.prototype.foo = function () { };
for (var propertyName in { bar: 1}) {
alert(propertyName);
}

and it will alert 'foo' as well as 'bar'.
 
T

Thomas 'PointedEars' Lahn

As I know,it's not well to extend Object.prototype derictly.

Depends. So far, whereever prototype objects are extended in a library,
precautions must be taken that the newly added enumerable properties are
excluded from for...in iteration and ignored with the `in' operation if so
desired.

I think it is OK to augment a built-in prototype object (including
Object.prototype) as long as the vendor of the library documents that and
provides means to work around this issue (say, a forEach() method accepting
an object and a callback reference.)
In the Prototype(JS Framewoke),there is no extend Object.prototype.
It only add some static method for Object class.

That only appears so. There are no classes and no static methods in the
ECMAScript implementations Prototype.js supports. They "extend" the Object
constructor, i.e. augment to the Function object it refers to with
user-defned properties. Adding properties to an object is a fairly normal
operation with prototype-based inheritance as there is no class binding to
forbid that.

They do add properties to several other built-in objects though, including
Number.prototype, String, String.prototype, and Array.prototype. That
latter breaks for...in iteration and the `in' operation if not explicitly
excluded by the programmer who is using that library. That programmer would
then have to wade through the obfuscated library code he is using in order
to make sure he covers all the library-defined properties, because the
library itself provides no such means.
I want to konw the reason.

See the signature below.
Who can give me some advise!?

Don't use Prototype.js or libraries based on it.


PointedEars
 
D

dhtmlkitchen

for..in enumeration will enumerate the properties added to
Object.prototype, try e.g.

Object.prototype.foo = function () { };
for (var propertyName in { bar: 1}) {
alert(propertyName);

}

and it will alert 'foo' as well as 'bar'.
This is my thinking.

Google: Object.prototype is verboten!

Until ECMAScript provides access to the DontEnum flag,
Object.prototype should not be modified.

Adding a static property to Object constructor function would not be
harmful.

Object.poop = "hearin aid umbrella";

But that would be fairly useless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top