ES5 function & ES6 class & method type

ES5 function

  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-06-04
  8. * @modified
  9. *
  10. * @description ES5 constructor function
  11. * @augments
  12. * @example
  13. * @link
  14. *
  15. */
  16. const log = console.log;
  17. // ES5 constructor function
  18. function Person(name, age) {
  19. // ES5 property
  20. this.uuid = `${name}_` + new Date().getTime();
  21. this.name = name;
  22. this.age = age;
  23. // ES5 instance method
  24. this.getName = function() {
  25. const name = `name: ${this.name}`;
  26. log(name)
  27. return name;
  28. };
  29. this.getAge = function() {
  30. const age = `age: ${this.age}`;
  31. log(age)
  32. return age;
  33. };
  34. this.getInfos = function(){
  35. const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
  36. log(`infos`, infos)
  37. return infos;
  38. }
  39. }
  40. // ES5 prototype property
  41. Person.prototype.gender = `man`;
  42. // ES5 prototype method
  43. // Person.prototype.getInfos = function(){
  44. // const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
  45. // log(`infos`, infos)
  46. // return infos;
  47. // }
  48. // static method
  49. Person.getUUID = function(person) {
  50. // const uuid = this.uuid;
  51. const uuid = person.uuid;
  52. log(uuid)
  53. return uuid;
  54. }
  55. const p = new Person(`elite`, 23);
  56. // call instance method
  57. p.getName();
  58. // call prototype method
  59. p.getInfos();
  60. // call static method
  61. // Person.getUUID();
  62. Person.getUUID(p);
  63. // name: elite
  64. // infos name: elite, age: 23, gender: man
  65. // undefined
  66. // elite_1591279465961
  67. // append after instance & ES5 prototype method
  68. Person.prototype.getGender = function(){
  69. const gender = `gender: ${this.gender}`;
  70. log(gender)
  71. return gender;
  72. }
  73. p.getGender();
  74. // gender: man
  75. // append after instance & ES5 static method
  76. Person.getSex = function(person){
  77. const sex = `sex: ${person.gender}`;
  78. log(sex)
  79. return sex;
  80. }
  81. Person.getSex(p);
  82. // sex: man

ES6 class

  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-06-04
  8. * @modified
  9. *
  10. * @description ES6 class
  11. * @augments
  12. * @example
  13. * @link
  14. *
  15. */
  16. const log = console.log;
  17. // ES6 class
  18. class Person {
  19. // ES6 constructor method
  20. constructor(name, age) {
  21. // ES6 property
  22. this.uuid = `${name}_` + new Date().getTime();
  23. this.name = name;
  24. this.age = age;
  25. }
  26. // ES6 instance method
  27. getName() {
  28. const name = `name: ${this.name}`;
  29. log(name)
  30. return name;
  31. };
  32. getAge() {
  33. const age = `age: ${this.age}`;
  34. log(age)
  35. return age;
  36. };
  37. // static method
  38. static getUUID(person) {
  39. // const uuid = this.uuid;
  40. const uuid = person.uuid;
  41. log(uuid)
  42. return uuid;
  43. }
  44. getInfos() {
  45. const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
  46. log(`infos`, infos)
  47. return infos;
  48. }
  49. }
  50. // ES6 prototype property
  51. Person.prototype.gender = `man`;
  52. // ES6 prototype method
  53. // Person.prototype.getInfos = function() {
  54. // const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
  55. // log(`infos`, infos)
  56. // return infos;
  57. // }
  58. const p = new Person(`elite`, 23);
  59. // call instance method
  60. p.getName();
  61. // call prototype method
  62. p.getInfos();
  63. // call static method
  64. // Person.getUUID();
  65. Person.getUUID(p);
  66. // name: elite
  67. // infos name: elite, age: 23, gender: man
  68. // undefined
  69. // elite_1591280013432
  70. // append after instance & ES6 prototype method
  71. Person.prototype.getGender = function(){
  72. const gender = `gender: ${this.gender}`;
  73. log(gender)
  74. return gender;
  75. }
  76. p.getGender();
  77. // gender: man
  78. // append after instance & ES6 static method
  79. Person.getSex = function(person){
  80. const sex = `sex: ${person.gender}`;
  81. log(sex)
  82. return sex;
  83. }
  84. Person.getSex(p);
  85. // sex: man

method type

es5 constructor & es6 class

  1. 静态方法 / static 方法, 直接在 ES5 constructorES6 class 上添加的方法

  2. 实例方法 / property 方法, 直接在 ES5 constructorES6 class 内添加的方法

  3. 原型方法 / prototype 方法, === 实例方法, 直接在 ES5 constructorES6 class 的 prototype 上添加的方法

  4. 私有方法 / private 方法,

  5. 保护方法 / protected 方法,

prototype

继承,多态


https://kangax.github.io/compat-table/es6/

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


ES5 function & ES6 class & method type的更多相关文章

  1. ES6 arrow function vs ES5 function

    ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...

  2. React入门 (1)—使用指南(包括ES5和ES6对比)

    前言 本篇会简明扼要的介绍一下React的使用方法.代码会用JSX+ES5和JSX+ES6两种方式实现. React简介 React来自Facebook,于2013年开源.至今不断修改完善,现在已经到 ...

  3. React访问组件元素的子元素(ES5与ES6的对比)

    // 创建组件var NewDom = React.createClass({ // 类名一定要大写开头 render: function () { return ( <ol> { Rea ...

  4. ES5 vs ES6

    ES5中 var React = require('react-native'); ES6中 import React from 'react-native'; .babelrc文件中添加一下内容 { ...

  5. JavaScript面向对象轻松入门之封装(demo by ES5、ES6、TypeScript)

    本章默认大家已经看过作者的前一篇文章 <JavaScript面向对象轻松入门之抽象> 为什么要封装? 封装(Encapsulation)就是把对象的内部属性和方法隐藏起来,外部代码访问该对 ...

  6. 多角度对比 ES5与ES6的区别

    ES5与ES6的对比不同点整理 本文关键词:ES6,javascript, 1.Default Parameters(默认参数) es6之前,定义默认参数的方法是在一个方法内部定义 var link ...

  7. ES5和ES6那些你必须知道的事儿(一)

    ES5和ES6那些你必须知道的事儿 ES5新增的东西 一.数组方法 1.forEach     用途:遍历,循环 对于空数组不会执行回调函数 //用法 array.forEach( function( ...

  8. ES5和ES6作用域

    ES5和ES6作用域 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. javaScript - 面向对象 - ES5 和 ES6

    javaScript - 面向对象 - ES5 和 ES6 ES5之前用 构造函数 构造函数的特点 就是一个普通函数, 他的函数名要大写.: 带方法的写法: 原型的方式: prototype 为内置的 ...

随机推荐

  1. 3分钟搞懂什么是WPF。

    先推荐下猛哥(刘铁猛)的书籍  <深入浅出WPF>. 一直以来,完美的用户体验是桌面应用程序和Web应用程序中的一大障碍.许多开发人员绞尽脑汁将界面设计得美观炫丽些.互 动感强些,但费了九 ...

  2. LOJ10099矿场搭建

    HNOI 2012 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖 ...

  3. SparkSQL访问Hive源,MySQL源

    SparkSQL访问Hive源,MySQL源 一.SparkSQL访问Hive源 软件环境 SparkSQL命令行模式可以直接连接Hive的 Java程序SparkSQL连接Hive 二.SparkS ...

  4. Centos7 yum 安装 oracle-rdbms-server-11gR2-pre

    Oracleyum官方网站 http://yum.oracle.com/ 一.下载yum源 根据自己需求下载相应的yum源 http://yum.oracle.com/getting-started. ...

  5. Spring boot 自定义注解标签记录系统访问日志

    package io.renren.common.annotation; import java.lang.annotation.Documented; import java.lang.annota ...

  6. Java使用反射的通用数组复制方法

    Java通用数组复制方法 在Arrays工具类中,提供了一个copyOf(T[] original, int newLength)方法,用于复制任意类型的对象数组,但是由于泛型不能作用于基本类型,所以 ...

  7. ES6(三) Promise 的基本使用方式

    基本用法 关于Promise的资料,网上有很多了,这里简单粗暴一点,直接上代码. 假设我们要做一个访问后端API的函数,那么我们可以这样模拟一下. const mySend = (url, data) ...

  8. vuex-pathify 一个基于vuex进行封装的 vuex助手语法插件

    首先介绍一下此插件 我们的目标是什么:干死vuex 我来当皇上!(开个玩笑,pathify的是为了简化vuex的开发体验) 插件作者 davestewart github仓库地址 官方网站,英文 说一 ...

  9. 2019 Multi-University Training Contest 1 Path(最短路+最小割)

    题意:给你n个点 m条边 现在你能够堵住一些路 问怎样能让花费最少且让1~n走的路比最短路的长度要长 思路:先跑一边最短路 建一个最短路图 然后我们跑一边最大流求一下最小割即可 #include &l ...

  10. OpenStack Train版-2.安装keystone身份认证服务

    安装 keystone 认证 mysql -uroot create database keystone; grant all privileges on keystone.* to 'keyston ...