类声明和类表达式


  ES6 中的类实际上就是个函数,而且正如函数的定义方式有函数声明和函数表达式两种一样,类的定义方式也有两种,分别是:类声明、类表达式。

类声明

  类声明是定义类的一种方式,就像下面这样,使用 class 关键字后跟一个类名(这里是 Ploygon),就可以定义一个类。

'use strict';
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
var p = new Polygon(10,20);
console.log('高度为:'+p.height);//10
console.log('宽度为:'+p.width);//20

  

变量提升

  类声明和函数声明不同的一点是,函数声明存在变量提升现象,而类声明不会。也就是说,你必须先声明类,然后才能使用它,否则代码会抛出 ——ReferenceError 异常,像下面这样:

'use strict';

var p = new Polygon(10,20);
console.log('高度为:'+p.height);
console.log('宽度为:'+p.width);
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
} var p = new Polygon(10,20);
^ ReferenceError: Polygon is not defined
at Object.<anonymous> (/demo.js:3:13)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3

  练习:

请声明一个类,命名为(Person),构造函数(constructor)中定义name属性,并定义一个实例方法sayName(){...}中打印'My name is'+this.name ,new Person('Lily'),调用sayName()方法,看结果。

'use strict';
class Person {
constructor(name){
this.name = name;
}
sayName(){
console.log("My name is"+this.name);
}
} var p1 = new Person("张三");
p1.sayName();

  运行结果:

>   
> My name is张三

类表达式


类表达式是定义类的另外一种方式,就像函数表达式一样,在类表达式中,类名是可有可无的。如果定义了类名,则该类名只有在类体内部才能访问到。

'use strict';
// 匿名类表达式
var Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
}; // 命名类表达式
var Polygon = class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
};

  

构造函数


  类的成员需要定义在一对花括号 {} 里,花括号里的代码和花括号本身组成了类体。类成员包括类构造器和类方法(包括静态方法和实例方法)。

  class 根据 constructor 方法来创建和初始化对象。

  constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类只能有一个constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。

constructor() {}

  constructor方法默认返回实例对象(即this),完全可以指定返回另外一个对象。

'use strict';
class Foo {
constructor() {
return Object.create(null);
}
}
console.log(new Foo() instanceof Foo);
运行结果:
>
> false

  

上面代码中,constructor函数返回一个全新的对象,结果导致实例对象不是Foo类的实例。

  constructor 方法是一个特殊的类方法,它既不是静态方法也不是实例方法,它仅在实例化一个类的时候被调用。一个类只能拥有一个名为 constructor 的方法,否则会抛出 SyntaxError 异常。

严格模式

类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式。只要你的代码写在类或模块之中,就只有严格模式可用。

静态方法


static关键字定义了一个类的静态方法。静态方法被称为无需实例化类也可当类被实例化。静态方法通常用于为应用程序创建实用函数。

示例

'use strict';
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
} static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y; return Math.sqrt(dx*dx + dy*dy);
}
} const p1 = new Point(5, 5);
const p2 = new Point(10, 10); console.log(Point.distance(p1, p2));

  

使用 extends 关键字创建子类


extends 关键字可以用来创建继承于某个类的子类。

这个例子是根据名为Animal类创建一个名为Dog的类。

'use strict';
class Person{ constructor(name){ this.name = name; } sayName(){ console.log('My name is'+this.name); } } class Tom extends Person{ sayName(){ console.log('My name is'+this.name); } } var tom = new Tom('tt');
tom.sayName(); 运行结果: >
> My name istt

  

												

NodeJS学习笔记二的更多相关文章

  1. nodejs学习笔记二:解析express框架项目文件

    上一章介绍了如何去创建一个express框架的工程项目,这章介绍一下express框架下的文件和用法解析,上一张我们创建的工程项目结构图如下: models是不属于原工程项目结构,为了实现数据模型后添 ...

  2. nodejs学习笔记二——链接mongodb

    a.安装mongoose库用来链接mongodb数据库 安装mongodb数据库参考mongodb安装 前言(怨言) 本来是想安装mongodb库来链接mongodb的,命令行到nodejs工程目录: ...

  3. nodejs学习笔记<二>简单的node服务器

    在环境搭建好后,就可以开始动手架设(node驱动)一个简单的web服务器. 首先,nodejs还是用js编写.先来看一段node官网上的实例代码. var http = require('http') ...

  4. nodejs学习笔记<二> 使用node创建基础服务器

    创建服务器的 server.js 内容. var http = require("http"); // 引用http模块 http.createServer(function(re ...

  5. nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)

    请求数据 前台:form.ajax.jsonp 后台:接受请求并返回响应数据     前台<= http协议 =>后台   常用的请求的方式: 1.GET           数据在url ...

  6. Nodejs学习笔记(二)——Eclipse中运行调试Nodejs

    前篇<Nodejs学习笔记(一)——初识Nodejs>主要介绍了在搭建node环境过程中遇到的小问题以及搭建Eclipse开发Node环境的前提步骤.本篇主要介绍如何在Eclipse中运行 ...

  7. Nodejs学习笔记(四)——支持Mongodb

    前言:回顾前面零零碎碎写的三篇挂着Nodejs学习笔记的文章,着实有点名不副实,当然,这篇可能还是要继续走着离主线越走越远的路子,从简短的介绍什么是Nodejs,到如何寻找一个可以调试的Nodejs ...

  8. Nodejs学习笔记(三)——一张图看懂Nodejs建站

    前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试 ...

  9. NodeJS学习笔记之Connect中间件模块(一)

    NodeJS学习笔记之Connect中间件模块(一) http://www.jb51.net/article/60430.htm NodeJS学习笔记之Connect中间件模块(二) http://w ...

随机推荐

  1. Jmeter content-type:multipart/form-data温故

    本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...

  2. 【RF库Collections测试】Copy Dictionary

    Name: Copy DictionarySource:Collections <test library>Arguments:[ dictionary ]Returns a copy o ...

  3. Qt监控后台服务运行状态

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMa ...

  4. memset和memcpy函数、atoi函数

    memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...

  5. js插件---->jquery通知插件toastr的使用

    toastr是一款非常棒的基于jquery库的非阻塞通知提示插件,toastr可设定四种通知模式:成功,出错,警告,提示,而提示窗口的位置,动画效果都可以通过能数来设置.toastr需要jquery的 ...

  6. 使用synchronized(非this对象)同步代码块解决脏读问题

    首先通过示例来学习验证多个线程调用同一个方法时随机的. package syn_out_asyn; import java.util.ArrayList; import java.util.List; ...

  7. JS-利用ajax获取json数据,并传入页面生成动态tab

    封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...

  8. LeetCode——Balanced Binary Tree

    Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...

  9. change事件的兼容性问题

    当input的value被修改时,在没有失去焦点的情况下,无法触发change事件,但是可以触发propertychange事件. 但是propertychange事件存在兼容性问题: IE9以下支持 ...

  10. Python语音合成

    注意:通过win32com调用的windows的SAPI,所以本脚本只适应于windows平台 代码很简单 #coding:utf-8 import win32com.client import ti ...