ES5 function & ES6 class & method type
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
静态方法 / static 方法, 直接在
ES5 constructor或ES6 class上添加的方法实例方法 / property 方法, 直接在
ES5 constructor或ES6 class内添加的方法原型方法 / prototype 方法, === 实例方法, 直接在
ES5 constructor或ES6 class的 prototype 上添加的方法私有方法 / private 方法,
保护方法 / protected 方法,
prototype
继承,多态
https://kangax.github.io/compat-table/es6/
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
ES5 function & ES6 class & method type的更多相关文章
- ES6 arrow function vs ES5 function
ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...
- React入门 (1)—使用指南(包括ES5和ES6对比)
前言 本篇会简明扼要的介绍一下React的使用方法.代码会用JSX+ES5和JSX+ES6两种方式实现. React简介 React来自Facebook,于2013年开源.至今不断修改完善,现在已经到 ...
- React访问组件元素的子元素(ES5与ES6的对比)
// 创建组件var NewDom = React.createClass({ // 类名一定要大写开头 render: function () { return ( <ol> { Rea ...
- ES5 vs ES6
ES5中 var React = require('react-native'); ES6中 import React from 'react-native'; .babelrc文件中添加一下内容 { ...
- JavaScript面向对象轻松入门之封装(demo by ES5、ES6、TypeScript)
本章默认大家已经看过作者的前一篇文章 <JavaScript面向对象轻松入门之抽象> 为什么要封装? 封装(Encapsulation)就是把对象的内部属性和方法隐藏起来,外部代码访问该对 ...
- 多角度对比 ES5与ES6的区别
ES5与ES6的对比不同点整理 本文关键词:ES6,javascript, 1.Default Parameters(默认参数) es6之前,定义默认参数的方法是在一个方法内部定义 var link ...
- ES5和ES6那些你必须知道的事儿(一)
ES5和ES6那些你必须知道的事儿 ES5新增的东西 一.数组方法 1.forEach 用途:遍历,循环 对于空数组不会执行回调函数 //用法 array.forEach( function( ...
- ES5和ES6作用域
ES5和ES6作用域 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- javaScript - 面向对象 - ES5 和 ES6
javaScript - 面向对象 - ES5 和 ES6 ES5之前用 构造函数 构造函数的特点 就是一个普通函数, 他的函数名要大写.: 带方法的写法: 原型的方式: prototype 为内置的 ...
随机推荐
- Tomcat优化,JNDI,连接池,数据源
什么是JNDI? JNDI的简单应用 什么是连接池技术? 连接池 性能 连接池技术与传统数据库连接的比较 连接池技术工作原理 为什么使用连接池? 传统数据库连接方式的不足 企业级开发需要稳健和高效的数 ...
- Java 8新特性(Lambda,Stream API)
由于最近总监要求学习Java 8的一些知识,就去网上找了 一套教程来学习学习,将学习结果做一个小的总结记录,方便以后使用: 1.Java 8的优点 2.Lambda表达式优点 2.1Lambda实例 ...
- vmware安装linux系统,自动建立没选项
虚拟机安装CentOS自己跳过分区,直接就到最后的软件包安装了 建完系统后不用power on,建完后在edit一下系统参数,应该会看见两个cd, 有一个是vmware自己加的,把那个删除后在开机就可 ...
- CCF-I'm stuck!(BFS)
I'm stuck! 问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七个字符中的一个,分别表示如下意思: '#': 任何 ...
- 利用selenium抓取网页的ajax请求
部门需要一个自动化脚本,完成web端界面功能的冒烟,并且需要抓取加载页面时的ajax请求,从接口层面判断请求是否成功.查阅了很多资料都没有人有过相关问题的处理经验,在处理过程中也踩了很多坑,所以如果你 ...
- Linux下安装MySQL及远程连接MySQL
安装方式一:通过下载官方安装包安装 由于Linux安装MySQL会遇到各种依赖问题,本博文整理了下安装方放,避免遇到依赖问题 查看是否自带mariadbrpm -qa|grep mariadb然后卸载 ...
- F - Courses (学生选课(匈牙利算法模板))
题目大意:一共有N个学生跟P门课程,一个学生可以任意选一门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: ...
- HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
- Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)
题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色. 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是 ...
- Redis 事务 & 消息队列
Redis 消息队列介绍 什么是消息队列 消息队列(Message Queue)是一种应用间的通信方式,消息发送后可以立即返回,有消息系统来确保信息的可靠传递,消息生产者只管把消息发布到消息队列中而不 ...