简单学ES6 - class
前言
随着ES6标准的定稿,众多的特性也趋于稳定,各大浏览器也在逐步实现这些特性,那么对ES6有更多的了解就无可厚非了。
准备
在学习ES6之前,我们需要有一个环境来测试ES6代码。在这里我推荐使用node的分支io.js。
如何安装?
- 下载地址:https://iojs.org/en/index.html,如果各位小伙伴不习惯英文,可以把url中的en修改为cn。
- 然后根据自己的操作系统版本,下载合适的安装包(主要指Windows系统)进行安装。
- 安装过程就不一一赘述了,和一般软件一样。
如何验证安装成功?
- 打开cmd,然后输入
iojs -v
,如果输出一个版本号,那么就代表io.js安装成功。(PS:我现在使用的是v1.2.0) - 也可以输入
iojs -p process.versions.v8
查看iojs所使用的V8(PS:不是V8发动机)的版本。(PS:我这儿显示4.1.0.14)
- 打开cmd,然后输入
小窥ES6
在测试ES6代码前,我们可以先看下io.js对ES6的支持:https://iojs.org/cn/es6.html。
接下来,开始我们的ES6-Class之旅:
1、class 基础
大家应该知道,在大部分面向对象的语言中,都有class的说法,那么在早期的Js中,面向对象的实现比较特殊,我们必须要用function来模拟。如:
//ES5及以下
function Point(x, y){
this.x = x;
this.y = y;
}
var p1 = new Point(100, 100);
然而在ES6中,我们可以直接使用class关键字,如:
//ES6
'use strict' //不能去掉,要不然iojs会提示不认识class。
class Point{
constructor(x, y){
this.x = x;
this.y = y;
}
}
var p1 = new Point(100, 100);
console.log(p1);
将以上代码保存为1.js,那么执行如下命令:iojs --es_staging 1.js
就可以看到"{x:100, y: 100}"这个结果了。(PS:注意要在1.js的目录打开cmd)。
接下来,看一个复杂点的,继承:
//ES6
'use strict'
class Point{
constructor(x, y){
this.x = x;
this.y = y;
}
}
var p1 = new Point(100, 100);
console.log(p1);
class ColorPoint extends Point{
constructor(x, y, color){
super(x, y);
this.color = color;
}
}
var cp = new ColorPoint(50, 50, 'red');
console.log(cp);
//输出继承关系
console.log(cp instanceof ColorPoint); //true
console.log(cp instanceof Point); //true
可以看到,和大部分语言的继承都很类似,如果你有其他面向对象语言的基础,那么很容易就能理解。
对Point和ColorPoint进行typeof,结果很明显也能看到是function。
console.log(typeof Point); // function
console.log(typeof ColorPoint); // function
那如果对class进行函数调用呢?
Point(100, 100); //Error
如上,必须通过new调用class,直接使用函数调用则会报错。
再来对比以下代码:
//标准的函数可以先写调用语句,后写申明语句。因为会定义前置
foo();
function foo(){}
//如果是class呢?
new Foo(); //Error,Foo is not defined
class Foo{}
如上,如果是定义的class,那么必须要定义语句在前,调用在后。
再来看以下的情形:
function funThatUseBar(){
new Bar();
}
//funThatUseBar(); //Error,Bar is not defined
class Bar{}
funThatUseBar(); //ok
如上,如果先使用了Bar,那么也是会报错的。必须要优先定义class。
附上以上所有的js,会报错的语句,进行了注释。
//ES6
'use strict'
class Point{
constructor(x, y){
this.x = x;
this.y = y;
}
}
var p1 = new Point(100, 100);
console.log(p1);
class ColorPoint extends Point{
constructor(x, y, color){
super(x, y);
this.color = color;
}
}
var cp = new ColorPoint(50, 50, 'red');
console.log(cp);
//*********************************************
//输出继承关系
console.log(cp instanceof ColorPoint); //true
console.log(cp instanceof Point); //true
console.log(typeof Point); // function
console.log(typeof ColorPoint); // function
//Point(100, 100); //Error
//************************************
//标准的函数可以先写调用语句,后写申明语句。因为会定义前置
foo();
function foo(){}
//如果是class呢?
//new Foo(); //Error,Foo is not defined
class Foo{}
//*******************************************
function funThatUseBar(){
new Bar();
}
//funThatUseBar(); //Error,Bar is not defined
class Bar{}
funThatUseBar(); //ok
2、类中的主体
ES6中、class的主体只能包含方法,不能包含数据属性。如果在类中包含变量定义,则会报错。class中的方法有三种类型:构造函数、静态方法、原型方法,如:
class Class1{
//构造
constructor(options){
}
// 静态方法,静态方法用static修饰
static staticMethod(){
return 'static method';
}
prototypeMethod(){
return 'prototype method';
}
}
其中,每个class和class原型的constructor都是相等的,同时class本质也是function
console.log(Class1 === Class1.prototype.constructor) // true
console.log(typeof Class1) // function
然后我们对类中的方法做测试
var p = console.log;
p(typeof Class1.prototype.prototypeMethod);
Class1.prototype.prototypeMethod() // 原型方法调用方式
p(typeof Class1.staticMethod);
Class1.staticMethod() //静态方法调用方式
Getters 和 Setters 的用法
class Class2{
get name(){
return 'jay';
}
set name(value){
console.log('set name = ' + value);
}
}
var c2 = new Class2();
c2.name = 'hu'; // "set name = hu"
console.log(c2.name); // "jay"
当使用了get和set时,那么针对属性的get和set会自动调用class中相关的方法。
贴出所有Js代码:
'use strict'
class Class1{
//构造
constructor(options){
}
// 静态方法
static staticMethod(){
return 'static method';
}
prototypeMethod(){
return 'prototype method';
}
}
console.log(Class1 === Class1.prototype.constructor);
console.log(typeof Class1);
var p = console.log;
p(typeof Class1.prototype.prototypeMethod);
p(typeof Class1.staticMethod);
class Class2{
get name(){
return 'jay';
}
set name(value){
console.log('set name = ' + value);
}
}
var c2 = new Class2();
c2.name = 'hu';
console.log(c2.name);
3、类的继承
简单的继承关系,如下:
'use strict'
class Class1{
toString(){
return 'parent class.';
}
}
class SubClass extends Class1{
toString(){
return 'sub class.';
}
}
var sc = new SubClass();
console.log(sc.toString()); // "sub class"
其中,sc是Class1的实例,也是SubClass的实例:
console.log(sc instanceof Class1); //true
console.log(sc instanceof SubClass); //true
如果要调用父类的方法,怎么办呢?
class SubClass2 extends Class1{
toString(){
return super.toString();
}
}
var sc2 = new SubClass2();
console.log(sc2.toString());
在继承关系中,子类的原型等于父类:
console.log(Object.getPrototypeOf(SubClass2) === Class1); //true
在子类中访问父类构造,使用super即可。
其他
- 如果想一览所有的ES6新特性,可以参考https://github.com/lukehoban/es6features
- 如果想系统的学习ES6,那么推荐http://es6.ruanyifeng.com/
- 想了解更多Classes in ECMAScript 6,可参考http://www.2ality.com/2015/02/es6-classes-final.html
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
简单学ES6 - class的更多相关文章
- 【转】用systemJS+karma+Jasmine+babel环境去编写简单的ES6工程
原文链接:http://www.cnblogs.com/shuoer/p/7779131.html 用systemJS+karma+Jasmine+babel环境去编写简单的ES6工程 首先解释下什么 ...
- 简单看看es6解构赋值
哎,我真的是太难了,今天就被这个解构赋值(也可以叫做析构,貌似析构是在c++中的,所以我这里叫做解构赋值吧)弄的我很烦,本来以为很容易的,结果还是弄了好久...就总结一下解构吧! 1.解构的基本使用 ...
- 简单学C——第四天
数组 在学数组之前,有必要把前面的知识复习一遍,当然我的复习,仅仅只是提一下,而对于你,则应该认真的看一下前面的知识点,不懂可以百度,哈哈. 前面我们大致学了 1.定义变量,2.数据的输入与输出,3. ...
- 简单学C——第二天
控制结构(-) 相信大家对流程图肯定很熟悉.下面我将介绍的正是关于此方面的,c语言中,控制结构大体分为选择结构和循环结构. 一.选择结构: 先贴出一般用于选择结构的语 ...
- 简单学c——前言
1.学C语言需要什么基础吗? 零基础. 2.什么是C语言? C语言是一种编程语言. 3.什么是编程语言? 编程语言是用来定义计算机程序的形式语言,是一种被标准化的交流技巧,用来向计算机发出指令. ...
- 简单说说ES6新特性
ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了. 它的目标,是使得 JavaScript 语言可以用来编写复杂的大型 ...
- 简单学JAVA之---接口的定义与实现
为了巩固自己学习的知识,可能会对自己以后所学的知识做一个小结,今天就来一篇接口的定义与实现. 在java中,我们可以通过继承得到另一个类中的方法,但是这个仅仅满足继承一个方法,那有办法可以多个继承不, ...
- 简单学C——第七天
函数 函数是C语言重要的组成部分,你现在,或者以后(如果C没什么变化的话)所写的任何一个C语言程序都是由一个一个的函数组合在一起的,当然,现在或许你只会在主函数 main中写一个小程序,那么在看了本篇 ...
- 简单学C——第五天
结构体 首先明确,结构体是一种构造的数据类型,是一种由多个数据类型如 int,char,double,数组或者结构体......组成的类型,现在告诉大家如何定义一个结构体.在定义int整型变量时,大家 ...
随机推荐
- Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)
第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Lifer ...
- shell日期的应用
#!bin/bash del_table() { #月初的第一天 month_first_day=`date +%Y%m01` #要删除的日期 last_7day_ago=`date -d " ...
- 阅读开发高手的代码 分享二则.NET开发框架的技巧
最近阅读了一套ERP开发框架的源代码,对开发框架的理解又深入一层,也为其将知识点运用的如此灵活而自叹不如. 郎咸平教授说,国际金融炒家对国际金融知识的理解与运用程序,是不可想像的.1997年的亚洲金融 ...
- (转)【Unity3d】Glow + Outline 轮廓描边
转:http://www.cnblogs.com/dosomething/archive/2012/08/04/2622488.html [Unity3d]Glow + Outline 轮廓描边 轮廓 ...
- zz A list of open source C++ libraries
A list of open source C++ libraries < cpp | links http://en.cppreference.com/w/cpp/links/libs Th ...
- 闲聊Redshift与日本CG行业的近况
最近不少朋友跟我说Redshift如何如何,恰巧我目前工作的工作室花费了巨资购买了Redshift和Quadro M4000,妄图在艺术家工作站上做一个新的动画项目,把渲染时间控制在15分钟以下.结果 ...
- git .gitignore 文件 解决二进制文件冲突问题
.gitignore 主要是添加 忽略文件 .最近团队开发经常出现 UserInterfaceState.xcuserstate 冲突,打开发现是二进制文件 ,没法解决冲突. 只好 rm -rf 之 ...
- SVO实时全局光照优化(里程碑MK0):Sparse Voxel Octree based Global Illumination (SVO GI)
完全自主实现,bloat-free.再次声明,这不是UE.U3D.CE.KlayGE! 老规矩,先贴图.后面有时间再补充描述. 1. 支持多跳间接全局光照2. 支持vxao/so.vxdiff/spe ...
- Scala 深入浅出实战经典 第78讲:Type与Class实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...
- npm install -g 全局安装总是出现permission权限问题的解决方案
npm install -g 全局安装总是出现permission权限问题的解决方案 开始使用node的时候,在使用npm安装global packages时,习惯性地使用npm install -g ...