如何创建包装器

发布于 2024-12-29 03:03:35 字数 270 浏览 3 评论 0原文

当我在 Google 上搜索特定内容时,我多次遇到“创建包装器并扩展它”的概念。就我而言,我想扩展 DOM。我知道人们多么反对这样做,但我想做的事情略有不同,要做到这一点,我至少需要探索这些方法。因此,无论我查看什么博客/wiki/tut,都没有得到直接答案,我向您提出的问题是:什么是包装对象/函数以及如何制作和使用它?

抱歉,如果事实证明我说得毫无道理;当我读到原型库和 JQuery 曾经做过此类事情时,我得到了这个想法。如果您能使用 DOM 作为示例,我们将不胜感激。

As I Googled particular things, I've numerously run into the concept of "create a wrapper and extend that." In my case, I want to extend DOM. I know how advised against that is, but what I'm trying to do is slightly different and to do it I need to at-least explore these methodologies. So my question to you, after not getting a straight answer nomatter what blog/wiki/tut I looked at, is: What is a wrapper object/function and how do you make and use one?

Sorry if it turns out I made no sense; I'm getting this idea from when I read that prototype library and JQuery did things of these sorts once upon a time. If you could use DOM as an example, it would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-05 03:03:35

愚蠢的例子,假设

MyDiv = {
  _e:document.getElementById('my-div'),
  setText:function(s) {
    this._e.innerText = s;
  },
  getText:function() {
    return this._e.innerText;
  },
  red:function() {
    this._e.style.backgroundColor = 'red';
  }
}

将 DOM 元素 my_div 包装在 MyDiv 内允许您可以过滤(或增强)包装对象的接口。

MyDiv.setText('Hello');
MyDiv.red();
alert(MyDiv.getText());

Silly example, assuming <div id='my-div'></div>

MyDiv = {
  _e:document.getElementById('my-div'),
  setText:function(s) {
    this._e.innerText = s;
  },
  getText:function() {
    return this._e.innerText;
  },
  red:function() {
    this._e.style.backgroundColor = 'red';
  }
}

Wrapping the DOM element my_div inside MyDiv allows you to filter (or augment) the wrapped object's interface.

MyDiv.setText('Hello');
MyDiv.red();
alert(MyDiv.getText());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文