前端組件化-抽象公共組件類

優化上次的組件化小demo

上次的組件化demo只是為了簡單的實現前端組件化的思想,這次我們稍微優化一下抽離公共類

下面代碼

html

<div id="wrapper"></div>
復制代碼

js

/* DOM字符串轉DOM節點 */
const createStringToDom = str => {const ele = document.createElement('div');ele.innerHTML = str;return ele;
}/* Mount */
const mount = (component, wrapper) => {wrapper.appendChild(component._renderDom());component.changeState = (oldEl, newEl) => {wrapper.insertBefore(newEl, oldEl);wrapper.removeChild(oldEl);}
}/* Components */
class Components {constructor (props = {}) {this.props = props;}setState (context) {const oldEl = this.el;Object.assign(this.state, context)this._renderDom();if (this.changeState) {this.changeState(oldEl, this.el)}}_renderDom () {this.el = createStringToDom(this.render());this.el.addEventListener('click', this.clickEvent.bind(this), false);return this.el;}
}/* ZanButton */
class ZanButton extends Components {constructor (props) {super(props);this.state = {isLike: false}}clickEvent () {this.setState({isLike: !this.state.isLike})}render () {return (`<button style="background-color: ${ this.props.bgColor }">${ this.state.isLike ? '取消' : '點贊' }</button>`)}
}const wrapper = document.getElementById('wrapper');
mount(new ZanButton(), wrapper);
mount(new ZanButton({bgColor: 'red'
}), wrapper);
復制代碼

通過抽離公共類繼續拆分代碼,使得組件復用更簡單容易,DOM操作也是自動完成的無需我們的介入,不過這里還有一個問題就是不斷的刪除,插入DOM節點會引起頁面的重排,加大開銷,這個問題通過 Virtual DOM 可以解決。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275159.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275159.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275159.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

時隔一年半,我,一個卑微的前端菜雞,又來寫面經了

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信ruochuan12 進群參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行4個月了&#xff0c;很多小伙伴表示收獲頗豐。作者&#xff1a;刮涂層_贏大獎原文地址…

javascript模版引擎-tmpl的bug修復與性能優化

http://www.planeart.cn/?p1594 http://ejohn.org/blog/javascript-micro-templating http://bbs.phpchina.com/thread-224712-1-1.html [ Noevil: 下面直接貼出改進好的MicroTemp&#xff0c;但是還是建議看一下原文&#xff0c;里面有詳細的改進細節&#xff0c;和改進前后的…

2019.5.8_此書真乃寶書也_從定位參數到僅限關鍵字參數

《摘自流暢的Python》 此書真乃寶書也,雖說還是有點兒沒懂 從定位參數到僅限關鍵字參數 Python最好的特性之一是提供了極為靈活的參數處理機制&#xff0c;而且Python3進一步提供了僅限關鍵字參數(keyword-only argument)。與之密切相關的是&#xff0c;調用函數時使用*和**“展…

用戶體驗與可用性測試_可用性作為用戶體驗的原則

用戶體驗與可用性測試Every UX Designer has his views and best practices. We all have a guide book created through time and experience. I want to share mine with you.每個UX設計器都有他的觀點和最佳實踐。 我們都有一本通過時間和經驗編寫的指南。 我想和你分享我的…

Jenkins插件之Deploy

deploy插件&#xff1a; Deploy Plugindeploy插件支持將War/Jar部署到遠程的應用服務器上&#xff0c;例如Tomcat,JBoss,Glassfish。正在尋找或開發.NET web 應用的自動發布插件。如何回滾或重新部署先前的build&#xff1a;0&#xff09; 需要被deploy的job的結果要存檔&#…

受美國法律保護美國妞_為什么美國法律有效地要求所有軟件設計都要響應

受美國法律保護美國妞Smashing Magazine defines “responsive design” as an approach where design responds to the user’s behavior and environment based on screen size, platform, and orientation. In responsive design, a breakpoint is the “point” at which a…

源碼群友問:你這么多項目是怎么進行技術選型的?

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信ruochuan12 進群參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行4個月了&#xff0c;很多小伙伴表示收獲頗豐。源碼群有群友提問我是怎么找到那么多npm…

iOS開發之打包上傳報錯: ERROR ITMS-90087/ERROR ITMS-90125

制作好的framework在打包上傳至AppStore如果出現以下錯誤&#xff0c;則說明這個SDK里面包含了x86_64, i386 架構&#xff0c;當然這個AppStore是不允許的&#xff0c;所以會在上傳的時候報錯&#xff0c;解決辦法就是要這個SDK剔除掉x86_64, i386這兩個架構 解決辦法&#xff…

[轉]cocos2d游戲開發,常用工具集合

cocos2d游戲開發&#xff0c;常用工具集合 原文地址&#xff1a;http://www.cocoachina.com/bbs/read.php?tid-68951.html 位圖字體工具Bitmap Font ToolsBMFont (Windows)FonteditorGlyph DesignerHieroLabelAtlasCreator粒子編輯工具Particle Editing ToolsParticleCreat…

拓展視野學習前端,我推薦這些

眾所周知&#xff0c;關注公眾號可以了解學習掌握技術方向&#xff0c;學習優質好文&#xff0c;落實到自己項目中。還可以結交圈內好友&#xff0c;讓自己融入到積極上進的技術氛圍&#xff0c;促進自己的技術提升。話不多說&#xff0c;推薦這些優質前端公眾號前端下午茶拓寬…

ios 動畫設計_動畫和講故事在設計中的力量

ios 動畫設計As human beings, we’ve always been fond of storytelling. Just think of campfire stories, Santa Claus, or that thrilling book you just finished. Here’s how you can use the power of storytelling to make your designs better.作為人類&#xff0c;我…

poj 2696 A Mysterious Function

A Mysterious FunctionTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3517 Accepted: 2398Description For any integers p and q with q > 0, define p mod q to be the integer r with 0 < r < q ?1 such that p?r is divisible by q. For example,…

面試官:能不能手寫一個 Promise?

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信ruochuan12 進群參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行4個月了&#xff0c;很多小伙伴表示收獲頗豐。以下問題你是不是在哪里聽過&#xff1f…

設計圖像素和開發像素_游戲開發的像素藝術設計

設計圖像素和開發像素Pixel art is a large part of the legacy of game development. Every Pokemon game up until their X/Y series was rendered entirely with pixel art, Ragnarok Online (2000) was one of the first commercial works to feature 3D rendering along s…

CF1100F Ivan and Burgers

CF1100F Ivan and Burgers 靜態區間&#xff0c;選取任意個數使得它們的異或和最大 \(n,\ m\leq5\times10^5,\ a_i\in[0,\ 10^6]\) lxl ST表&#xff0c;線性基 如果暴力維護線性基&#xff0c;線段樹時間復雜度為 \(O(n\log^2n)-O(\log^3n)\) 由于重復元素對答案沒有影響&…

深入淺出Nintex——更新PeopleandGroup類型的Field

轉載于:https://www.cnblogs.com/mingle/archive/2011/11/25/2263199.html

從 vue-cli 源碼中,我發現了27行讀取 json 文件有趣的 npm 包

1. 前言大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行四個月了&#xff0c;很多小伙伴表示收獲頗豐。想學源碼&#xff0c;極力推薦訂閱我寫…

自定義view示例_自定義404頁的10個示例(從最佳到最差)

自定義view示例自定義404頁面 (Custom 404 pages) To customize or not to customize your 404 page? I hope by now you know the answer is that, yes, under essentially all circumstances you should customize your 404 page. 404 errors occur when someone attempts t…

BTF:實踐指南

本文地址&#xff1a;BTF&#xff1a;實踐指南 | 深入淺出 eBPF 1. BPF 的常見限制 1.1 調試限制1.2 可移植性2. BTF 是什么&#xff1f;3. BTF 快速入門 3.1 BPF 快速入門3.1 BTF 和 CO-RE4. 結論 BPF 是 Linux 內核中基于寄存器的虛擬機&#xff0c;可安全、高效和事件驅動…

python 混入類MixIn

寫在前面 能把一件事情說的那么清楚明白&#xff0c;感謝廖雪峰的官方網站。 1.為什么要用混入類&#xff1f;&#xff08;小白入門&#xff09; 繼承是面向對象編程的一個重要的方式&#xff0c;因為通過繼承&#xff0c;子類就可以擴展父類的功能。 step1: 回憶一下Animal類層…