1.OOP 指什么?有哪些特性
面向?qū)ο缶幊?/strong> ( Object-Oriented Programming, 縮寫:OOP ) 是一種程序設(shè)計(jì)思想。它是指將數(shù)據(jù) 封裝進(jìn)對(duì)象中 。然后操作對(duì)象, 而不是數(shù)據(jù)自身,是適用于面向?qū)ο蟮? 它是基于原型的模型。
OPP的特性:繼承性,封裝性,多態(tài)性
2. 如何通過構(gòu)造函數(shù)的方式創(chuàng)建一個(gè)擁有屬性和方法的對(duì)象?
1.構(gòu)造函數(shù) 2.new運(yùn)算符實(shí)例化
var Animal = function(){
this.種類 = '動(dòng)物'
}
Animal.prototype.say = function(){
console.log('動(dòng)物叫')
}
var cat = new Animal()
cat.say()
3. prototype 是什么?有什么特性
Javascript規(guī)定,每一個(gè)構(gòu)造函數(shù)都有一個(gè)prototype屬性,指向另一個(gè)對(duì)象。這個(gè)對(duì)象的所有屬性和方法,都會(huì)被構(gòu)造函數(shù)的實(shí)例繼承。
默認(rèn)情況下prototype屬性會(huì)默認(rèn)獲得一個(gè)constructor(構(gòu)造函數(shù))屬性,這個(gè)屬性是一個(gè)指向prototype屬性所在函數(shù)的指針,通過構(gòu)造函數(shù)new出來的對(duì)象都有prototype屬性,都指向同一個(gè)prototype屬性,利用這個(gè)特性,可以把對(duì)象的公共方法寫在原型對(duì)象中,這樣,所有new 出來的對(duì)象都可以繼承公共方法。
4.畫出如下代碼的原型圖

image.png
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(){
console.log('run')
}
Car.prototype.stop = function(){
console.log('stop')
}
Car.prototype.getStatus = function(){
console.log('status')
}
var car = new Car('xxx', 'yellow', 'good')
car.run()
car.stop()
car.getStatus()