1.OOP是指什么?有哪些特性
OOP : Object Oriented Programming 面向?qū)ο缶幊淌敌兀渲袃蓚€最重要的概念就是類和對象他嫡。類只是具備了某些功能和屬性的抽象模型,類在實例化之后得到的實體就是對象
特性:
- 繼承:子類自動繼承父類中的屬性和方法庐完,并可以添加新的屬性和方法或者對部分屬性進行重寫钢属。
- 封裝“: 隱藏對象的屬性和實現(xiàn)細節(jié),將功能進行封裝门躯,僅對外開放接口
- 多態(tài):同一屬性或方法在不同的對象上有不同的表現(xiàn)形式淆党。
2.如何通過構造函數(shù)的方式創(chuàng)建一個擁有屬性和方法的對象
function Person(name){
this.name = name;
this.say =function(){
console.log("my name is" + this.name)
}
}
var xiaoming = new Person('xiaoming')
xiaoming.say() // my name is xiaoming
3.prototype是什么?有什么特性
javaScript中創(chuàng)建的每一個函數(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('小虎')
pro.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
this.run =function(){
console.log("this car is running")
}
this.stop =function(){
console.log("this car has stopped" )
}
this.getStatus = function(){
console.log("this car is" + this.status)
}
}
6.創(chuàng)建一個GoTop對象荷憋,當new一個GoTop對象則會在頁面上創(chuàng)建一個回到頂部的元素,點擊頁面滾動到頂部域醇。擁有以下屬性和方法
1. 'ct'屬性台谊,GoTop對應的DOM元素的容器
2. 'target'屬性,GoTop對應DOM元素
3. 'bindEvent'方法譬挚,用于綁定事件
4. 'creatNode'方法锅铅,用于在容器內(nèi)創(chuàng)建節(jié)點
function GoTop($ct){
this.ct = $ct
this.target =$( "<button>Top</button>")
this.creatNode = function(){
this.ct.append(this.target)
}
this.bindEvent =function(){
this.target.on("click",function(){
$(window).scroll(0)
})
}
this.creadNode();
this.bindEvent();
}
var goTop = new GoTop($(".gotop"))
效果→效果
7.使用木桶布局實現(xiàn)一個圖片墻
效果→效果