1.OOP 指什么艺挪?有哪些特性
OOP是指面向?qū)ο缶幊蘋bject-oriented programming不翩。面向?qū)ο笾凶钪匾氖穷惡蛯ο蟆n愂蔷邆淞四承┕δ芎蛯傩缘某橄竽P吐樯选6愂菍嵗缶褪菍ο蟆?/p>
特性:
1口蝠、繼承性
2、封裝性:將一個類的實現(xiàn)和使用分開津坑,只保留部分接口與外部聯(lián)系
3妙蔗、多態(tài)性:子類繼承了來自父級類中的屬性和方法,可以對其中方法進行重寫疆瑰。
2.如何通過構造函數(shù)的方式創(chuàng)建一個擁有屬性和方法的對象?
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是顯示原型對象昙啄,每一個函數(shù)對象都有prototype屬性,指向另一個對象寸五。這個對象的所有屬性和方法都會被構造的實例繼承梳凛。
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)建一個 Car 對象,擁有屬性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)建一個 GoTop 對象蹈集,當 new 一個 GotTop 對象則會在頁面上創(chuàng)建一個回到頂部的元素烁试,點擊頁面滾動到頂部。擁有以下屬性和方法
1. `ct`屬性拢肆,GoTop 對應的 DOM 元素的容器
2. `target`屬性减响, GoTop 對應的 DOM 元素
3. `bindEvent` 方法, 用于綁定事件
4. `createNode` 方法郭怪, 用于在容器內(nèi)創(chuàng)建節(jié)點
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()
【個人總結支示,如有錯漏,歡迎指出】
:>