2.用面向对象写程序的步骤

步骤:

    1.写一个类, 变量就是属性,并在里面执行init方法

    2.方法挂在原型上,不同的功能全部拆开,分别写在原型上。

    ( 建init方法初始化,里面写主逻辑。 )

    3.实例化对象




function Tab() {

this.btns= document.querySelectorAll('input')

this.divs= document.querySelectorAll('div')

this.init()

}

Tab.prototype.init = function(){

let that= this

    this.btns.forEach(function (ele,i) {

ele.onclick = function( ){

that.change(i)

}

})

}

Tab.prototype.change = function(i){

let that= this

    this.btns.forEach(function (e,i) {

e.className=''

        that.divs[i].className=''

    })

this.btns[i].className= 'active'

    this.divs[i].className= 'show'

}

lettab1 = new Tab();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容