What exactly is DOM Extension / Wrapping?

眉间皱痕 提交于 2019-12-20 11:32:04

问题


I have 2 main questions.

  1. Does extending things like Object count?

  2. What is DOM wrapping?

http://perfectionkills.com/whats-wrong-with-extending-the-dom/

After reading that article I couldn't find anything about DOM wrapping, and no specification and what exactly is and isn't DOM extension.


回答1:


No, Object is specified as part of the Javascript language, while the DOM is an API only relevant in a browser environment and is used to "access and update the content, structure and style of documents" (W3C).

However, one of the reasons provided in that article arguing against the extension of DOM objects still applies to extending native types such as Object - namely the chance of collisions.


Wrapping an object refers to creating a new object that references the original, but providing additional functionality through the new, wrapper object.

For example, rather than extending a DOM Element object with a cross-browser addClass function like this:

var element = document.getElementById('someId');
element.addClass = function (className) {
    ...
};

You can instead define a wrapper function:

var ElementWrapper = function (element) {
    this.element = element;
};

And add the function to its prototype:

ElementWrapper.prototype.addClass = function (className) {
    ...
};

And "wrap" elements like this:

var element = document.getElementById('someId');
var wrapped = new ElementWrapper(element);
wrapped.addClass('someClass');


来源:https://stackoverflow.com/questions/5318846/what-exactly-is-dom-extension-wrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!