ES6之面向对象简介
程序思维可以分为面向过程和面向对象两类:
面向过程是以功能为中心,面向对象是以对象为中心。
有一个简单的例子,把大象装进冰箱。两种不同的思想有不同的做法:
//面向过程
//打开冰箱门
function openFridge(){
}
//把大象放进去
function putElephant(){
}
putElephant()
//关上冰箱门
function closeFridge(){
}
closeFridge()
openFridge()
//面向对象
function Elephant(){}
function Fridge(){}
Fridge.prototype.openFridge=function(){}
Fridge.prototype.closeFridge=function(){}
Fridge.prototype.putElephant=function(elephant){
this.openFridge()
this.closeFridge()
}
const fridge=new Fridge()
fridge.putElephant(new Elephant())