Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is DOM Extension / Wrapping?

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.

like image 700
McKayla Avatar asked Mar 15 '11 22:03

McKayla


1 Answers

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');
like image 188
David Tang Avatar answered Oct 13 '22 00:10

David Tang