zoukankan      html  css  js  c++  java
  • Class和构造函数的异同

    以下两种方式实现相同的功能

    1. 在普通对象上面加两个属性和一个方法

    2. 在原型对象上加一个方法

    3. 在构造函数对象上面加一个方法

      class Student {
      constructor(name, age) {
      this.name = name ;
      this.age = age;
      this.sayName = this.sayName
      },
      sayName() {
      console.log(this.name)
      },
      static sayHello() {
      console.log('helloworld')
      }
      }

      function Student(name, age) {
      this.name = name ;
      this.age = age ;

       this.sayName = function() {
         console.log(this.name)
       }
      

      }

      Student.prototype.sayName = function() {
      console.log(this.name)
      }

      Student.sayHello = function() {
      console.log('helloworld')
      }

    区别

    1. 类的形式
      • 可以在Class内部同时定义普通对象的属性方法,定义构造函数对象上面的方法,定义原型对象上面的方法属性
      • 值得注意的是通过静态关键字只能在构造函数对象上面添加方法,也就是说只能定义静态的方法,不能定义静态的属性
    2. 构造函数的形式
      • 在构造函数内部只能定义普通对象上面的方法和属性
      • 静态方法也就是构造函数对象上面的方法,只能通过显式的追加
      • 原型对象上面的方法和属性也需要显式的追加



  • 相关阅读:
    EventBus
    Date 时间 日期 常用方法函数
    线程 Thread Handler
    MySQL-DoubleWrite
    MySQL各版本优化器变化
    MySQL优化器-条件过滤(condition_fanout_filter)
    PXC集群搭建
    mysql主从不一致--relay_log_recovery设置成0
    MySQL5.7-sql_mode
    根据ibd文件进行数据恢复或导入
  • 原文地址:https://www.cnblogs.com/chihaiping/p/7007912.html
Copyright © 2011-2022 走看看