問題1: OOP 指什么婿斥?有哪些特性
- OOP:
Object-oriented programming的縮寫劝篷,即面向?qū)ο蟪绦蛟O(shè)計,其中兩個最重要的概念就是類和對象民宿。類只是具備了某些功能和屬性的抽象模型娇妓,而實際應(yīng)用中需要一個一個實體,也就是需要對類進(jìn)行實例化活鹰,類在實例化之后就是對象哈恰。 - 特性:
(1)繼承性:子類自動繼承其父級類中的屬性和方法,并可以添加新的屬性和方法或者對部分屬性和方法進(jìn)行重寫志群。繼承增加了代碼的可重用性着绷。
(2)多態(tài)性:子類繼承了來自父級類中的屬性和方法,并對其中部分方法進(jìn)行重寫锌云。
(3)封裝性:將一個類的使用和實現(xiàn)分開蓬戚,只保留部分接口和方法與外部聯(lián)系。
問題2: 如何通過構(gòu)造函數(shù)的方式創(chuàng)建一個擁有屬性和方法的對象?
function People(name, age){
this.name = name;
this.age = age;
}
People.prototype.sayName = function(){
console.log(this.name)
}
var p1 = new People('hunger', '20');
p1.sayName();//hunger
問題3: prototype 是什么宾抓?有什么特性
prototype:每一個構(gòu)造函數(shù)都有一個prototype屬性子漩,指向另一個對象。這個對象的所有屬性和方法石洗,都會被構(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('前端');
問題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(this.name + ' is running');
}
Car.prototype.stop = function(){
console.log(this.name + ' is stopped');
}
Car.prototype.getStatus = function(){
console.log(this.name +' is '+ this.status);
}
var myCar = new Car('Maserati','red','running');
var yourCar = new Car('Ferrari','red','stopped')
myCar.run();
myCar.stop();
myCar.getStatus();
yourCar.run();
yourCar.stop();
yourCar.getStatus();
問題6: 創(chuàng)建一個 GoTop 對象,當(dāng) new 一個 GotTop 對象則會在頁面上創(chuàng)建一個回到頂部的元素枷畏,點(diǎn)擊頁面滾動到頂部别厘。擁有以下屬性和方法
1. `ct`屬性,GoTop 對應(yīng)的 DOM 元素的容器
2. `target`屬性拥诡, GoTop 對應(yīng)的 DOM 元素
3. `bindEvent` 方法触趴, 用于綁定事件
4 `createNode` 方法, 用于在容器內(nèi)創(chuàng)建節(jié)點(diǎn)
function GoTop(ct){
this.ct = $(ct);
this.creatNode = function(){
var target = $('<span class="item" style="border: 1px solid red">回到頂部</span>');
this.ct.append(target);
return target;
};
this.target=this.creatNode();
this.bindEvent = function(){
var _this = this;
this.target.click(function(){
$(window).scrollTop(0);
})
};
}
var gotop = new GoTop( '.ct' );
gotop.bindEvent();