對(duì)象、原型

1.OOP 指什么?有哪些特性

OOP是指面向?qū)ο缶幊蘋bject-oriented programming。面向?qū)ο笾凶钪匾氖穷惡蛯?duì)象。類是具備了某些功能和屬性的抽象模型。而類是實(shí)例化之后就是對(duì)象。

特性:
1、繼承性
2、封裝性:將一個(gè)類的實(shí)現(xiàn)和使用分開,只保留部分接口與外部聯(lián)系
3、多態(tài)性:子類繼承了來自父級(jí)類中的屬性和方法,可以對(duì)其中方法進(jìn)行重寫。

2.如何通過構(gòu)造函數(shù)的方式創(chuàng)建一個(gè)擁有屬性和方法的對(duì)象?

function People(name,age){
    this.name = name;
    this.age = age;
    }
}

People.prototype.sayName = function(){
    console.log('name:'+ this.name)
}

var p1 = new People('jack','20')

p1.sayName();//name:jack

3. prototype 是什么?有什么特性

prototype是顯示原型對(duì)象,每一個(gè)函數(shù)對(duì)象都有prototype屬性,指向另一個(gè)對(duì)象。這個(gè)對(duì)象的所有屬性和方法都會(huì)被構(gòu)造的實(shí)例繼承。

4.畫出如下代碼的原型圖

function People (name){
  this.name = name;
  this.sayName = function(){
    console.log('my name is:' + this.name);
  }
}

People.prototype.walk = function(){
  console.log(this.name + ' is walking');  
}

var p1 = new People('小八');
var p2 = new People('前端');
原型圖.png

5.創(chuàng)建一個(gè) Car 對(duì)象,擁有屬性name、color、status;擁有方法run,stop,getStatus

function Car(name,color,status){
    this.name = name;
    this.color = color;
    this.status = status;
}

Car.prototype.run = function(){
    console.log('run')
}
Car.prototype.stop = function(){
    console.log('stop')
}
Car.prototype.getStatus = function(){
    console.log(this.status);
}

var myCar = new Car('jack','blue','running')

6.創(chuàng)建一個(gè) GoTop 對(duì)象,當(dāng) new 一個(gè) GotTop 對(duì)象則會(huì)在頁面上創(chuàng)建一個(gè)回到頂部的元素,點(diǎn)擊頁面滾動(dòng)到頂部。擁有以下屬性和方法

1. `ct`屬性,GoTop 對(duì)應(yīng)的 DOM 元素的容器
2.  `target`屬性, GoTop 對(duì)應(yīng)的 DOM 元素
3.  `bindEvent` 方法, 用于綁定事件
4. `createNode` 方法, 用于在容器內(nèi)創(chuàng)建節(jié)點(diǎn)

function GoTop(){
    this.ct = $('.ct');
    this.target = $('<p class="gotop">回到頂部<p>');
    this.creaNode()
    this.bindEvent()
}

GoTop.prototype = {
    bindEvent:function(){
        this.target.on('click',function(){
            $(window).scrollTop(0)
        })
        var _this = this
        $(window).on('scrollTop',function(){
            if($(this).scrollTop()>500){
                _this.target.show()
            }else{
                _this.target.hide()
            }
        })
    }
    createNode:function(){
        this.ct.append(this.target)
    }
}
var go1 = new GoTop()
go1.bindEvent()
go1.createNode()

【個(gè)人總結(jié),如有錯(cuò)漏,歡迎指出】
:>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容