js学习第16 天

####  面向过程:

   程序 = 算法+语法;

   算法:强调步骤,一步接一步

   缺陷:

    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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值