問(wèn)題1: OOP 指什么?有哪些特性
- Object-oriented programming的縮寫,即面向?qū)ο蟪绦蛟O(shè)計(jì)。
- 特性
- 繼承性:子類自動(dòng)繼承其父級(jí)類中的屬性和方法,并可以添加新的屬性和方法或者對(duì)部分屬性和方法進(jìn)行重寫。繼承增加了代碼的可重用性。
- 多態(tài)性:子類繼承了來(lái)自父級(jí)類中的屬性和方法,并對(duì)其中部分方法進(jìn)行重寫。
- 封裝性:將一個(gè)類的使用和實(shí)現(xiàn)分開,只保留部分接口和方法與外部聯(lián)系。
問(wèn)題2: 如何通過(guò)構(gòu)造函數(shù)的方式創(chuàng)建一個(gè)擁有屬性和方法的對(duì)象?
function Preson(name,age){ //屬性
this.name = name;
this.age = age
}
Preson.prototype.printName = function(){ //方法
console.log(this.name);
}
var p = new Preson('yasinchan', 100);
問(wèn)題3: prototype 是什么?有什么特性
- prototype對(duì)象是實(shí)現(xiàn)面向?qū)ο蟮囊粋€(gè)重要機(jī)制。每個(gè)函數(shù)也是一個(gè)對(duì)象,它們對(duì)應(yīng)的類就是function,每個(gè)函數(shù)對(duì)象都具有一個(gè)子對(duì)象prototype。Prototype 表示了該函數(shù)的原型,prototype表示了一個(gè)類的屬性的集合。當(dāng)通過(guò)new來(lái)生成一個(gè)類的對(duì)象時(shí),prototype對(duì)象的屬性就會(huì)成為實(shí)例化對(duì)象的屬性。
- 任何函數(shù)都有 prototype 屬性,對(duì)應(yīng)的值是一個(gè)對(duì)象,叫做原型對(duì)象。
問(wèn)題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('前端');

4.png
問(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(){}
Car.prototype.stop = function(){}
Car.prototype.getStatus = function(){}
問(wèn)題6: 創(chuàng)建一個(gè) GoTop 對(duì)象,當(dāng) new 一個(gè) GotTop 對(duì)象則會(huì)在頁(yè)面上創(chuàng)建一個(gè)回到頂部的元素,點(diǎn)擊頁(yè)面滾動(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)