How to create a packer

As I searched for specific things, I repeatedly came across the concept of "create a wrapper and expand it." In my case, I want to extend the DOM. I know how this is recommended, but what I'm trying to do is a little different, and for this I need to at least study these methodologies. So, my question to you, after I did not get a direct answer to the question which blog / wiki / tut I looked at, is: What is a shell object / function and how do you use and use it?

Sorry if it turns out I didn't make any sense; I get this idea from when I read this prototype library, and jQuery once did similar things. If you could use the DOM as an example, that would be appreciated.

+4
source share
1 answer

Stupid 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 complement) the interface of the wrapped object.

 MyDiv.setText('Hello'); MyDiv.red(); alert(MyDiv.getText()); 
+13
source

Source: https://habr.com/ru/post/1393246/


All Articles