首页 > 其他分享 >es6 class 实践

es6 class 实践

时间:2022-11-29 20:02:11浏览次数:30  
标签:function es6 instanceA console log 实践 classA class

Class in ES6

从es6开始引入了class这个语法糖,针对babel,或者tsc,转码后,类会变成什么样,这篇文章将阐述编译后的结果。

首先看看es5中的类的实现,举个栗子

function classA(){
  this.a='a';
  this.printA=function(){
    console.log(this.a);
  }
  let b='b';
  function printB(){
    console.log('invisible to child')
  }
}

let instanceA=new classA();
instanceA.a;   // 'a', 实例本身的属性
instanceA.printA()  //'a', 实例本身的方法

instanceA.b  // undefined
instanceA.printB()  // underfined

instanceA.__proto__=== classA.prototype   // true
instanceA.__proto__  //  {constructor:function classA(){xxxx}}

classA.prototype.printDoubleA=function(){console.log(`${this.a}${this.a}`)};
instanceA.printDoubleA()  //  'aa';

classA.staticPrint=function(){console.log('this is static print')}
instanceA.staticPrint()  // underfined

总结一下:

  • 在调用new classA 之前,构建这个对象 this={};this.__proto__={consturctor:function classA(){xxx}};
  • 然后相当于classA.call(this)
  • 由于classA没有返回值,所以,this就是实例。classA 中凡是赋值给this的,都将变成实例的属性。其他的对于实例而言,都是不可见的。
  • 构建实例后,如果给classA.prototype添加新的属性,那么对于实例而言,立刻可见
  • 如果给classA添加其他属性,那么对于实例而言就是不可见的,而添加的属性相当于类里面的static属性。

下面我来看一下es6中的类,通过babel 转码后变成es5会变成啥样,同样举个栗子

class ClassA{
  a;  
  printA(){
    console.log(this.a);
  }
  b='b';
  printB=function(){console.log(this.b)};
  static c='c';
  static printC(){
    console.log(ClassA.c);
  }
}

转码后的如下,

function ClassA(){
  this.a=null;
  this.b='b';
  this.printB=function(){console.log(this.b);}
}
ClassA.prototype.printA=function(){console.log(this.a)}
ClassA.c='c';
ClassA.printC=function(){console.log(ClassA.c)}

总结一下

  • es6中类的语法糖是通过原型链实现的
  • es6中类带有初始化的属性,最终会出现在类实例上面,包括属性,函数
  • es6中定义的函数,将出现在es5类函数的原型链上面
  • es6中定义的静态属性,将出现在es5类函数对象本身上面。

最后看看typescript,效果跟上面的babel 一样

class Base {
  a: string = '';
  printA() {
    console.log(this.a);
  }
  b: string = '';
  printB = () => {
    console.log(this.b);
  }
  static printC() {
    console.log('hello C');
  }
}

总结一下。

  • 平时基本上沉溺于typescript,基本上不写js。因为实在是太香了。所以对于babel的转码基本不用。这次算是补补功课。

标签:function,es6,instanceA,console,log,实践,classA,class
From: https://www.cnblogs.com/kongshu-612/p/16936520.html

相关文章

  • Class文件解析
    1准备工作获取class文件byte[]publicstaticbyte[]getFileBytes(Filefile){try(FileInputStreamfileInputStream=newFileInputStream(file)){......
  • rfc3442-classless-static-routes 字段含义
    参考文档:https://www.nuomiphp.com/serverfault/zh/60480e758726d95932321906.html#dhcp配置optionrfc3442-classless-static-routescode121=arrayofinteger......
  • Android 调试桥:adb的入门与最佳实践(无线连接调试)
    商业转载请联系作者获得授权,非商业转载请注明出处。目录​​一adb简介​​​​二 adb工作原理​​​​三配置adb环境​​​​四常用的adb命令​​​​4.1help命令​​......
  • 一文带你快速入门 Go 语言微服务开发 - Dubbo Go 入门实践总结
    更多详细示例可直接访问Dubbo官网或搜索关注官方微信公众号:ApacheDubbo1.安装Go语言环境建议使用最新版go1.17goversion>=go1.15【Go语言官网下载地址】......
  • Function源码解析与实践
    作者:陈昌浩1导读if…else…在代码中经常使用,听说可以通过Java8的Function接口来消灭if…else…!Function接口是什么?如果通过Function接口接口消灭if…else…呢?让我们一......
  • 找call的万能方法,一招通杀所有网络游戏【个人实践出来的】
    呵呵这个说的是方法,就不拿哪个游戏来举例了,懂得原理,操作自己灵活变动。首先说的,网络游戏是有客户端服务器的,这样就得需要一个通讯,基本上的功能都会通过服务器来验证......
  • Dockerfile最佳实践
    目录1.一份简单的Dockerfile参考模板2.构建高效镜像生命周期2.1镜像build2.2镜像pull3.常见问题3.1注意Dockerfile中的指令是逐条执行,且相互独立3.2提防“过度”缓存3.3ARG......
  • 【转】C语言表驱动法编程实践
    来源:C语言表驱动法编程实践(精华帖,建议收藏并实践)(qq.com)数据压倒一切。如果选择了正确的数据结构并把一切组织的井井有条,正确的算法就不言自明。编程的核心是数据结......
  • Function源码解析与实践
    作者:陈昌浩1导读if…else…在代码中经常使用,听说可以通过Java8的Function接口来消灭if…else…!Function接口是什么?如果通过Function接口接口消灭if…else…呢?......
  • NoClassDefFoundError原因排查
    NoClassDefFoundError错误发生的原因 NoClassDefFoundError错误的发生,是因为Java虚拟机在编译时能找到合适的类,而在运行时不能找到合适的类导致的错误。例如在运行时我们......