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ò)漏,歡迎指出】
:>