Detailed explanation of js appearance mode

零到壹度
Release: 2018-03-23 10:07:44
Original
1258 people have browsed it

Appearance mode actually provides a higher-level unified interface, which is often used to handle compatibility unification or encapsulation, for example, processing binding events. The following two examples are taken as an example.

1. Encapsulate binding events:

function addEvent(dom,type,fn){ if(dom.addEventListener){ dom.addEventListener(type,fn,false) } else if(dom.attachEvent){ dom.attachEvent('on'+type,fn); } else { dom['on'+type]=fn; } } var myp = document.getElementById('myp'); addEvent(myp,'click',function(){ console.log('hahah'); })
Copy after login

2. You can also use appearance Pattern encapsulates multiple functions:

var A={ get:function(id){ return document.getElementById(id) } css:function(id,key,value){ this.get(id).style[key]=value; } html:function(id,content){ this.get(id).innerHTML=content; } A.html('box','这是新添加的内容');
Copy after login

Related recommendations:

JS Design Pattern - Appearance Pattern

JS Design Pattern Appearance Mode

js Appearance Mode

The above is the detailed content of Detailed explanation of js appearance mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!