ES5 function & ES6 class & method type

ES5 function

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-06-04
* @modified
*
* @description ES5 constructor function
* @augments
* @example
* @link
*
*/ const log = console.log; // ES5 constructor function
function Person(name, age) {
// ES5 property
this.uuid = `${name}_` + new Date().getTime();
this.name = name;
this.age = age;
// ES5 instance method
this.getName = function() {
const name = `name: ${this.name}`;
log(name)
return name;
};
this.getAge = function() {
const age = `age: ${this.age}`;
log(age)
return age;
};
this.getInfos = function(){
const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
log(`infos`, infos)
return infos;
}
} // ES5 prototype property
Person.prototype.gender = `man`; // ES5 prototype method
// Person.prototype.getInfos = function(){
// const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
// log(`infos`, infos)
// return infos;
// } // static method
Person.getUUID = function(person) {
// const uuid = this.uuid;
const uuid = person.uuid;
log(uuid)
return uuid;
} const p = new Person(`elite`, 23); // call instance method
p.getName(); // call prototype method
p.getInfos(); // call static method
// Person.getUUID();
Person.getUUID(p); // name: elite
// infos name: elite, age: 23, gender: man
// undefined
// elite_1591279465961 // append after instance & ES5 prototype method
Person.prototype.getGender = function(){
const gender = `gender: ${this.gender}`;
log(gender)
return gender;
} p.getGender();
// gender: man // append after instance & ES5 static method
Person.getSex = function(person){
const sex = `sex: ${person.gender}`;
log(sex)
return sex;
} Person.getSex(p);
// sex: man

ES6 class

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-06-04
* @modified
*
* @description ES6 class
* @augments
* @example
* @link
*
*/ const log = console.log; // ES6 class
class Person {
// ES6 constructor method
constructor(name, age) {
// ES6 property
this.uuid = `${name}_` + new Date().getTime();
this.name = name;
this.age = age;
}
// ES6 instance method
getName() {
const name = `name: ${this.name}`;
log(name)
return name;
};
getAge() {
const age = `age: ${this.age}`;
log(age)
return age;
};
// static method
static getUUID(person) {
// const uuid = this.uuid;
const uuid = person.uuid;
log(uuid)
return uuid;
}
getInfos() {
const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
log(`infos`, infos)
return infos;
}
} // ES6 prototype property
Person.prototype.gender = `man`; // ES6 prototype method
// Person.prototype.getInfos = function() {
// const infos = `name: ${this.name}, age: ${this.age}, gender: ${this.gender}`;
// log(`infos`, infos)
// return infos;
// } const p = new Person(`elite`, 23); // call instance method
p.getName(); // call prototype method
p.getInfos(); // call static method
// Person.getUUID();
Person.getUUID(p); // name: elite
// infos name: elite, age: 23, gender: man
// undefined
// elite_1591280013432 // append after instance & ES6 prototype method
Person.prototype.getGender = function(){
const gender = `gender: ${this.gender}`;
log(gender)
return gender;
} p.getGender();
// gender: man // append after instance & ES6 static method
Person.getSex = function(person){
const sex = `sex: ${person.gender}`;
log(sex)
return sex;
} Person.getSex(p);
// 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 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 再谈SQL Server中日志的的作用
  2. linux 常用命令学习记录
  3. Java学习随笔4:Java的IO操作
  4. 【Oralce】时间操作
  5. java控制台输入
  6. cvc-elt.1: Cannot find the declaration of element 'beans'
  7. win8 客户端源码
  8. 集中式(CVS、SVN)VS分布式(Git)
  9. Unsupported major.minor version 52.0错误解决 Ubuntu JDK8 安装配置
  10. cas单点登录系统:客户端(client)详细配置
  11. [ASP.NET MVC] Razor 布局
  12. 64、django之模型层(model)--建表、查询、删除基础
  13. SQLite事务与自增深度分析
  14. Android 实现形态各异的双向侧滑菜单 自定义控件来袭
  15. 前端js总结
  16. javascript的ES6学习总结(第二部分)
  17. vue 用checkbox 做多选,带选中样式
  18. 【webdriver自动化】整理API框架(主要是关键字,具体例子在本地)
  19. 【转】Django 图表制作(By Highcharts)
  20. MDK972-EK开发板裸调试设置和裸机程序烧写(转)

热门文章

  1. 微博CacheService架构浅析 对底层协议进行适配
  2. super 多重继承 super() function with multilevel inheritance
  3. LOJ10069 TREE
  4. PowerQuery合并查询原理
  5. (四)Springboot以jar包方式启动、关闭、重启脚本
  6. Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制
  7. Java性能优化,操作系统内核性能调优,JYM优化,Tomcat调优
  8. spark SQL(六)性能调整
  9. PyQt中ui编译成窗体.py,中文乱码
  10. KVM(虚拟机的迁移)