#### 面向过程:
程序 = 算法+语法;
算法:强调步骤,一步接一步
缺陷:
1. 随着代码规模的增加,问题逐渐难以控制
2. 复用性比较低,只能复用函数
面向对象:
程序 = 对象 + 对象 ... + 对象
对象:(简单理解)封装了若干属性和方法的一个变量
类:拥有相同属性和行为的对象的集合,类是一个模板
对象:是类的实例化,唯一且真实存在的
### es5创建类的方法
this的用法:this是函数的内置对象
1.与事件连用,代表触发该事件的元素
2.与普通方法连用(除了事件体和构造函数),代表调用该函数的对象
3.与构造方法连用,代表new出来的对象
### es6创建类的方法
class Student{
//属性:ES6中所有的属性定义都在constructor中
//构造
constructor(id,name,score){
this.id = id;
this.name = name;
this.score = score;
}
//方法
eat(){
console.log(this.name + "eat");
}
rap(){
console.log(this.name + "rap");
}
}
let s1 = new Student(1,"老王",100);
console.log(s1.id,s1.name,s1.score);
s1.eat();
s1.rap();
// 2. 编写一个加法器,它能够接收两个整数,然后输出两个整数的和。
function Add(num1,num2){
this.num1 = num1;
this.num2 = num2;
this.fun = function(){
console.log(this.num1+this.num2)
}
}
let eg = new Add(2,2)
eg.fun()
class Add1{
constructor(num1,num2){
this.num1 = num1;
this.num2 = num2;
}
fun(){
console.log(this.num1+this.num2)
}
}
let eg1 = new Add1(2,2);
eg1.fun()
</script>