問(wèn)題1: OOP 指什么?有哪些特性
OOP指的是面向?qū)ο缶幊獭>褪菍⑹挛锍橄蟪蓪?duì)象。
面向?qū)ο蟮娜齻€(gè)基本特征是:封裝、繼承、多態(tài)。
- 封裝,也就是把客觀事物封裝成抽象的類,并且類可以把自己的數(shù)據(jù)和方法只讓可信的類或者對(duì)象操作,對(duì)不可信的進(jìn)行信息隱藏。
- 繼承,它可以使用現(xiàn)有類的所有功能,并在無(wú)需重新編寫(xiě)原來(lái)的類的情況下對(duì)這些功能進(jìn)行擴(kuò)展。
- 多態(tài),允許將子類類型的指針賦值給父類類型的指針。實(shí)現(xiàn)多態(tài),有二種方式,覆蓋,重載。
問(wèn)題2: 如何通過(guò)構(gòu)造函數(shù)的方式創(chuàng)建一個(gè)擁有屬性和方法的對(duì)象?
function Car(brand){
this.brand = brand;
this.start = function(){
console.log('gogogo');
}
}
問(wèn)題3: prototype 是什么?有什么特性
js本身不提供一個(gè)class
的實(shí)現(xiàn),js對(duì)象都有一個(gè)私有屬性(稱之為 [[Prototype]]),它持有一個(gè)連接到另一個(gè)稱為其 prototype 對(duì)象(原型對(duì)象)的鏈接。該 prototype 對(duì)象又具有一個(gè)自己的原型,層層向上直到一個(gè)對(duì)象的原型為 null。JavaScript 中幾乎所有的對(duì)象都是位于原型鏈頂端的Object的實(shí)例。
問(wèn)題4:畫(huà)出如下代碼的原型圖
prototype
問(wèn)題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 run run');
}
Car.prototype.stop = function() {
console.log('stop');
}
Car.prototype.getStatus = function() {
return this.status;
}
問(wèn)題6: 創(chuàng)建一個(gè) GoTop 對(duì)象,當(dāng) new 一個(gè) GotTop 對(duì)象則會(huì)在頁(yè)面上創(chuàng)建一個(gè)回到頂部的元素,點(diǎn)擊頁(yè)面滾動(dòng)到頂部。擁有以下屬性和方法
function goTop($ct, $target){
this.$ct = $ct;
this.$target = $target;
this.createNode();
this.bindEvent();
}
goTop.prototype = {
bindEvent: function(){
this.$target.on('click', function(e){
e.preventDefault();
$('html,body').animate({
scrollTop: 0
}, 700);
})
},
createNode: function(){
this.$ct.append(this.$target);
}
}
new goTop($('body'), $('<a href="#">top</a>'));