#用面向對象封裝通用選項卡
###改成面向對象原則:
1. 全局變量就是屬性
2. 函數就是方法
3. onload中創建對象
4. 改this指向問題,在什么情況下this指向特別容易被修改(事件或者是定時器),盡量讓面向對象中的this指向對象
###復習this指向
```
oDiv.onclick = function(){
this: oDiv // 這里的this指的是oDiv
};
oDiv.onclick = show;
function show() {
this: oDiv // 這里的this指的還是oDiv
};
oDiv.onclick = function(){
show();
};
function show() {
this: window // 這里的this指的就是window
}
```
###用面向對象封裝通用選項卡
```
window.onload = function () {
var t1 = new Tab();
t1.init();
};
function Tab() {
this.oParent = document.getElementById('div1');
this.aInput = this.oParent.getElementsByTagName('input');
this.aDiv = this.oParent.getElementsByTagName('div');
}
Tab.prototype.init = function () {
var _this = this;
for (var i = 0; i < this.aInput.length; i++) {
this.aInput[i].index = i;
this.aInput[i].onclick = function () {
_this.change(this);
};
}
};
Tab.prototype.change = function (obj) {
for (var j = 0; j < this.aInput.length; j++) {
this.aInput[j].className = '';
this.aDiv[j].style.display = 'none';
}
obj.className = 'active';
this.aDiv[obj.index].style.display = 'block';
};
```
- 01 JS面向對象及組件開發
- 02 傳統的過程式編寫選項卡
- 03 用面向對象封裝通用選項卡
- 04 控制多個選項卡自動播放
- 05 用面向對象編寫拖拽
- 06 JS面向對象及組件開發
- 07 hasOwnProperty和constructor的使用
- 08 instanceof運算符的使用
- 09 利用toString做類型判斷
- 10 什么是面向對象的繼承
- 11 面向對象之拷貝繼承
- 12 編寫繼承的拖拽
- 13 繼承的其他形式之類式繼承
- 14 繼承的其他形式之原型繼承
- 15 組件開發是什么
- 16 給拖拽組件配置不同參數
- 17 封裝彈框組件
- 18 使用對象標記已彈出彈框
- 19 復雜組件開發之自定義事件
- 20 原生JS實現自定義事件
- 21 自定義事件實例
- 22 基于JQ的選項卡組件開發