最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - General reasons not to deal with Document's and Element's prototype - Stack Overflow

programmeradmin3浏览0评论

Are there general reasons not to deal with Document's and Element's prototype?

I like to create my own little framework, because my current project doesn't need the mass of features of the existing frameworks.

I don't need to support browsers which don't support Element/Document-constructor and also will not execute scripts that are not under my control.

So would you remend to extend the prototype or should I go the usual way and create own objects from Element/Document?

Are there general reasons not to deal with Document's and Element's prototype?

I like to create my own little framework, because my current project doesn't need the mass of features of the existing frameworks.

I don't need to support browsers which don't support Element/Document-constructor and also will not execute scripts that are not under my control.

So would you remend to extend the prototype or should I go the usual way and create own objects from Element/Document?

Share Improve this question edited Feb 26, 2015 at 18:06 Dr.Molle asked Sep 29, 2010 at 23:23 Dr.MolleDr.Molle 117k16 gold badges199 silver badges206 bronze badges 2
  • 1 It's really not clear what you are asking here. – Robusto Commented Sep 29, 2010 at 23:30
  • I talk about Element.prototype and Document.prototype(HTMLDocument.prototype). What are the reason's not to use them, if there are any? – Dr.Molle Commented Sep 29, 2010 at 23:54
Add a ment  | 

2 Answers 2

Reset to default 9

Do you plan to extend default DOM elements? If so, please don't. Juriy Zaytsev (aka Kangax) clearly describes why not in What’s wrong with extending the DOM.

Yes, unfortunately. It would be lovely to be able to add functionality by fiddling the DOM prototypes, but in practice it's just not reliable given today's technology.

Document, Element and others etc may be ‘host objects’ implemented by the browser with no ability to fiddle with their prototypes. Host objects may potentially have many other weird behaviours that a native JavaScript object wouldn't. DOM Nodes are host objects in IE6-7 and many older, niche and mobile browsers.

Even if they are implemented as native-JavaScript objects, there is no standard (yet) that described where the constructor-function for them is to be found, for you to go fishing about in the .prototype. Document, Element and so on are just W3 DOM interface names, they say nothing about what the implementation objects are to be found.

It happens that modern browsers (IE8 native mode and recent versions of Firefox, Opera and WebKit) do make constructor-functions available so you can start adding methods to Document or HTMLElement. But even then, there are differences between what objects are exposed, as not every browser provides the DOM interfaces with implementations under the same names. (The subinterfaces/implementations of NodeList are particularly troublesome.)

You can see how DOM prototyping has worked in practice by looking at the Prototype.js framework. When it works, it's super smooth. But because you can't prototype everywhere, you end up with some extremely ugly stuff where the framework has to deal with places prototyping won't work by copying methods into every instance of a Node. And then you've got the situation where your code might forget it needs to force this ‘augmentation’ and so it might work or not work depending on whether some other function happened to augment the same node before. This leads to really horrible browser-specific, interaction-order-specific, race-condition-prone debugging pain.

If you can limit your prototyping work to a few well-supported interfaces, and give up on all but the latest browsers, you can probably get away with it.

发布评论

评论列表(0)

  1. 暂无评论