年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1551|回复: 0

Javascript中ES5与ES6创建类的两种不同方式

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2018-1-13 15:52:53 | 显示全部楼层 |阅读模式
    1、ES5使用构造函数模式与原型模式相结合
    [JavaScript] 纯文本查看 复制代码
    // 构造函数模式
    function Person(name, age){
      this.name = name;
      this.age = age;
    }
    // 原型模式
    Person.prototype = {
      constructor:Person,
      print(){
        console.log('...');
      }
    }


    2、ES6中使用class关键字,类似高级语言
    [JavaScript] 纯文本查看 复制代码
    // class方法
    class Person{
      constructor(name, age){
        this.name = name;
        this.age = age;
      }
      print(){
        console.log("...");
      }
    }
    

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-19 01:46 , Processed in 0.043862 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表