如何创建包装器 [英] How to create a Wrapper

查看:94
本文介绍了如何创建包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我搜索特定的东西时,我已经多次遇到创建包装并扩展它的概念。就我而言,我想扩展DOM。我知道对此有何建议,但我想要做的是略有不同,为此我需要至少探索这些方法。所以我的问题是,在没有得到直接答案之后,我看到的是什么博客/ wiki / tut是:什么是包装器对象/函数以及如何制作和使用它?

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?

对不起,如果事实证明我毫无意义;当我读到原型库时,我从这个想法中得到了这个想法,JQuery曾经做过这些事情。如果您可以使用DOM作为示例,我们将不胜感激。

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.

推荐答案

愚蠢的例子,假设< ; 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';
  }
}

包装DOM元素 my_div 里面 MyDiv 允许你过滤(或扩充)包装对象的界面。

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());

这篇关于如何创建包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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