How to encapsulate plug-ins in native js
This time I will show you how to encapsulate plug-ins in native js. What are the precautions for encapsulating plug-ins in native js? The following is a practical case, let’s take a look.
Today I will introduce how to write your own plug-in. It is recommended to review it before reading itObject-oriented; I will write a simple plug-in to reset the style, without further ado. Upload the code;
//SetStyles.js (function(win, doc) { var defaultSettings = { color: "red", background: "blue", border: "2px solid #000", fontSize:"30px", textAlign:"center", width:"200px", borderRadius:"5px" }; function SetStyles(options) { var self = this; //没传配置项自己丢错 if(!options) { throw new Error("请传入配置参数"); } self = Object.assign(self, defaultSettings, options); self.container = doc.querySelector(self.container) || doc.querySelectorAll(self.container); self._changeStyles(); } SetStyles.prototype = { _changeStyles: function() { var self = this; for(var pro in self) { if(pro == "container") { continue; } if(pro == 'text' && typeof self[pro]== 'string') { self.container.innerText = self[pro]; continue; }else if(pro == 'text' && typeof self[pro]== 'function'){ self.container.innerText = self[pro](); continue; } self.container.style[pro] = self[pro]; } } } win.SetStyles = SetStyles; })(window, document) //调用 var a = new SetStyles({ container:"#test", background:"#fff", textAlign:"center", text:function(){ return "我是文本"; } }); //text参数格式字符串或者函数 //container用的querySelectAll方法,参数一致 //其他css参数为字符串
First define the next default parameter defaultSettings
Then write a
constructor, why do you need to use Object.assign to merge objects, because you may not necessarily write all the default configuration, don’t write Some will default to the default parameters, and some will choose the parameters you write, so the options are placed at the end; Finally, write the method in the prototype.
Methods are generally written in the prototype, and properties are written in the constructor.
Everyone should be able to understand the function of this code. It resets the css style, which is similar to jquery’s css() function.
But I don’t recommend you to use this. After all, follow the principle and try to use js as little as possible to operate the dom. After all, this cost is very expensive. I just write this to let everyone know how to encapsulate the plug-in and change the css style. It's better to write a few more classes. If you want to use that style, just change the class names.
What are the commonly used html element structures
How to solve the label and input spacing problem in Google Browse
What are the ways to disable page caching
How to use base64 encoding for html images instead
The above is the detailed content of How to encapsulate plug-ins in native js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PyCharm is a powerful and popular Python integrated development environment (IDE) that provides a wealth of functions and tools so that developers can write code more efficiently. The plug-in mechanism of PyCharm is a powerful tool for extending its functions. By installing different plug-ins, various functions and customized features can be added to PyCharm. Therefore, it is crucial for newbies to PyCharm to understand and be proficient in installing plug-ins. This article will give you a detailed introduction to the complete installation of PyCharm plug-in.
![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

According to news from this site on April 17, TrendForce recently released a report, believing that demand for Nvidia's new Blackwell platform products is bullish, and is expected to drive TSMC's total CoWoS packaging production capacity to increase by more than 150% in 2024. NVIDIA Blackwell's new platform products include B-series GPUs and GB200 accelerator cards integrating NVIDIA's own GraceArm CPU. TrendForce confirms that the supply chain is currently very optimistic about GB200. It is estimated that shipments in 2025 are expected to exceed one million units, accounting for 40-50% of Nvidia's high-end GPUs. Nvidia plans to deliver products such as GB200 and B100 in the second half of the year, but upstream wafer packaging must further adopt more complex products.

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Detailed explanation of how to install and set up the EclipseSVN plug-in Eclipse is a widely used integrated development environment (IDE) that supports many different plug-ins to extend its functionality. One of them is the EclipseSVN plugin, which enables developers to interact with the Subversion version control system. This article will detail how to install and set up the EclipseSVN plug-in and provide specific code examples. Step 1: Install the EclipseSVN plug-in and open Eclipse

PyCharm is a powerful Python integrated development environment. By installing plug-ins, you can further improve development efficiency and facilitate developers' work. This article will share some tips on PyCharm plug-in installation, so that you can get twice the result with half the effort, while also providing specific code examples to demonstrate how to use the plug-in. Step 1: Open PyCharm, click "File" in the menu bar, and then select "Settings". Step 2: In the Settings window, click "
