問題1: OOP 指什么锋恬?有哪些特性
OOP指:Object Oriented Programming,面向?qū)ο缶幊坦溃且环N解決代碼復用的設計和編程方法匪煌。這種方法讓相近相似的操作邏輯和操作應用數(shù)據(jù)、狀態(tài)裆悄,以類的型式描述出來矛纹,以對象實例的形式復用,以達到提高開發(fā)效率的作用光稼。
特性:
封裝性:種把數(shù)據(jù)和方法綁定在一起使用的方法
繼承性:子類自動繼承其父級類中的屬性和方法
多態(tài)性:不同的類可以定義相同的屬性或方法
問題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();
問題3: prototype 是什么或南?有什么特性
- 幾乎任何對象有一個prototype屬性,該屬性指向的是這個對象的原型艾君。
- 這個對象的原型像一個公用的場所采够,里面的所有屬性和方法,創(chuàng)建出來的所有實例都能繼承使用冰垄。
問題4:畫出如下代碼的原型圖
Paste_Image.png
原型對象.png
問題5: 創(chuàng)建一個 Car 對象蹬癌,擁有屬性name、color虹茶、status逝薪;擁有方法run,stop蝴罪,getStatus
function Car(name,color,status){
this.name = name;
this.clor = color;
this.status = status;
}
Car.prototype.run = function(){
console.log(this.name);
}
Car.prototype.stop = function(){
console.log(this.name);
}
Car.prototype.getStatus = function(){
console.log(this.name);
}
var oneCar = new Car('a','red','1');
var twoCar = new Car('b','blue','2');
oneCar.run();
oneCar.stop();
oneCar.getStatus();
twoCar.run();
twoCar.stop();
twoCar.getStatus();