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 为内置的 ...
随机推荐
- HTTP/2与HTTP/3的新特性
解密HTTP/2与HTTP/3的新特性 - InfoQ https://www.infoq.cn/article/kU4OkqR8vH123a8dLCCJ
- (转载)微软数据挖掘算法:Microsoft 神经网络分析算法(10)
前言 有段时间没有进行我们的微软数据挖掘算法系列了,最近手头有点忙,鉴于上一篇的神经网络分析算法原理篇后,本篇将是一个实操篇,当然前面我们总结了其它的微软一系列算法,为了方便大家阅读,我特地整理了一篇 ...
- Covering Indexes in MySQL, PostgreSQL, and MongoDB
Covering Indexes in MySQL, PostgreSQL, and MongoDB - Orange Matter https://orangematter.solarwinds.c ...
- copy,集合
一.基础数据类型补充: 1种方法:删除列表里面的元素时,一定不能循环列表,会出错.可以循环索引,然后循环删除开头或结尾这个位置的元素(原开头结尾的元素被删除之后,会有新的元素顶上来). 2种方法:把要 ...
- 济南学习D3T1__线性筛和阶乘质因数分解
[问题描述] 从1− N中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数最大可能是多少. [输入格式] 第一行一个数字N. [输出格式] 一行,一个整数代表答案对100000007取模之后的 ...
- 使用 Tye 辅助开发 k8s 应用竟如此简单(一)
最近正巧在进行 Newbe.Claptrap 新版本的开发,其中使用到了 Tye 来辅助 k8s 应用的开发.该系列我们就来简单了解一下其用法. Newbe.Claptrap 是一个用于轻松应对并发问 ...
- JavaWeb——Ajax与MVC学习总结
Ajax: 什么是Ajax? 为什么使用Ajax? 使用jquery Ajax实现登录 Ajax实例练习: 设计模式 设计模式的定义: 设计模式的作用: MVC设计模式 MVC设计模式的模块组成: M ...
- ArrayList源码分析(JDK1.8)
概述 ArrayList底层是基于数组实现的,并且支持动态扩容的动态数组(变长的集合类).ArrayList允许空值和重复的元素,当向ArrayList中添加元素数量大于其底层数组容量时,会通过扩容机 ...
- c++11新特性实战(二):智能指针
c++11添加了新的智能指针,unique_ptr.shared_ptr和weak_ptr,同时也将auto_ptr置为废弃(deprecated). 但是在实际的使用过程中,很多人都会有这样的问题: ...
- 国产App为什么如此“臃肿”?!
引言 App是Application的简称,正是因为有了丰富多彩的各类App,人们就可以通过它们来最大限度地发挥手中设备的功能.本文主要讨论手机上的App,因为手机的硬件和软件与十余年前相比早已有了巨 ...