DOM扩展/包装究竟是什么? [英] What exactly is DOM Extension / Wrapping?

查看:143
本文介绍了DOM扩展/包装究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主要问题。


  1. 扩展的东西像 Object count?

什么是DOM包装?

http://perfectionkills.com/whats-wrong-with-扩展的dom /

阅读该文章后,我找不到关于DOM包装的任何内容,没有规范,什么是和不是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.

推荐答案

否, Object 被指定为Javascript 语言,而DOM是仅在浏览器环境中相关的 API ,用于访问和更新文档的内容,结构和样式(W3C)

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).

但是,提供的原因之一在反对DOM对象扩展的文章仍然适用扩展本机类型,例如 Object - 即碰撞的机会。

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.

例如,而不是使用跨浏览器 addClass 函数扩展DOM Element对象,如下所示:

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) {
    ...
};

和换行元素如下:

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

这篇关于DOM扩展/包装究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆