当前位置: 移动技术网 > IT编程>开发语言>JavaScript > ES6入门教程之Class和Module详解

ES6入门教程之Class和Module详解

2018年05月01日  | 移动技术网IT编程  | 我要评论

本文主要介绍了es6中class和module的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

一、class

es6引入了class(类)这个概念,作为对象的模板。通过class关键字,可以定义类。

 // 定义类
 class point() {

  constructor(x, y) {
   this.x = x;
   this.y = y;
  }

  tostring() {
   return '(' + this.x + ', ' + this.y + ')';
  }
 }

 var point = new point(2, 3);
 point.tostring(); //(2, 3)

在上面的代码片段里,先是定义了一个point类,里面还有一个constructor函数,这就是构造函数。而this关键字则代表实例对象。

class之间可以通过extends关键字实现继承

class colorpoint extends point {
 constructor(x, y, color) {
  super(x, y); //等同于super.constructor(x, y)
  this.color = color;
 }

 tostring() {
  return this.color + '' + super();
 }
}

二、module的基本用法

1>、export和import

es6实现了模块功能,视图解决javascript代码的依赖和部署上的问题,取代现有的commonjs和amd规范,成为浏览器和服务器通用的模块解决方案。

模块的功能有两个关键字: export和import。export用于用户自定义模块。规定对外接口;import用于输入其他模块的功能,同时创建命名空间(namespace),防止函数名冲突。

es6允许将独立的js文件作为模块,也就是说,允许一个javascript脚本文件调用另一个脚本文件。最简单的模块就是一个js文件,里面使用export关键字输出变量。

 //profile.js
 export var firstname = "pandora";
 export var lastname = "g.dragon";
 export var year = 1973;

 //export还有下面这种写法,两者是等价的
 var firstname = "pandora";
 var lastname = "g.dragon";
 var year = 1973;
 export({firstname, lastname, year});

使用export定义模块之后,其他js文件就可以通过import关键字加载这个模块(文件)了。加载方式如下:

 import {firstname, lastname, year} from './profile';

 function setheader(element) {
  element.textcontent = firstname + '' + lastname;
 }

上面的代码片段中,使用了import关键字接受一个对象——用“{ }”表示。里面指定了要从其他模块中导入的变量。大括号里面的变量名必须与被导入模块对外接口的名称相同。

但是,如果要给输入的属性和方法重新取一个名字,import语句要写成下面这样。

 import {somemethod, another as newname} from './exporter';

2>、模块的整体加载

export关键字除了输出变量,还可以输出方法或类(class)。看看下面代码:

 // circle.js

 // 方法-1: 返回圆的面积
 export function area(radius) {
  return math.pi * radius * radius; 
 }
 // 方法-2: 返回圆的周长
 export function circumference(radius) {
  return 2 * mathi.pi * radius;
 }

下面,定义一个main.js文件引用上面的模块。

 // mian.js
 import {area, circumference} from 'circle';

 console.log("圆面积: " + area(4));
 console.log("圆周长: " + circumference(14));

上面的写法是逐一制定要导入的方法。但是还有另外一种写法,就是使用module关键字,整体导入。

 // main.js
 module circle from 'circle';

 console.log("圆面积: " + circle.area(4));
 console.log("圆周长: " + circle.circumference(14));

module关键字后面跟着一个变量,表示导入的模块定义在该变量上。

3>、export default语句

如果不想为某个属性或方法制定输入的名称,可以使用export default语句。

 // export-default.js
 export default function foo() { // foo()就是这个模块的默认方法
  console.log('foo');
 }

当在其它模块中导入该模块时,import语句可以为默认方法指定任意名字。

 // import-default.js
 import customname from './export-default';
 customname(); //'foo'

显然,一个模块只能由一个默认方法。
对于默认属性,则是直接定义在export default后面即可。如下代码所示:

 export default 42;

三、模块的继承

模块之间是可以继承的。

现在,假设一个circleplus模块继承了circle模块。代码如下:

 //circleplus.js
 export * from 'circle'; // "export *"表示输出circle模块的所有属性和方法
 export var e = 2.71828;
 export default function(x) {
  return math.exp( x );
 }

这时,可以对cicle中的属性和方法改名后再输出。

 export {area as circlearea } from 'circle';

加载模块的写法如下:

 //main.js
 module math from 'circleplus';
 import exp from "circleplus"; // "import exp"表示将circleplus模块的默认方法加载为exp方法。
 console.log( exp(math.pi) );

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网