ECMAScript6语法重点(二)
十一.Proxy和Reflect(Proxy保护对象不被外界访问;Object方法移植到Reflect)
①Proxy:原始对象(供应商)通过Proxy(代理商)生成新对象(映射原对象),用户访问的是新对象,对新对象操作会通过Proxy传回原对象,原对象对用户不可见。
{
let obj={
time:'2017-03-11',
name:'net',
_r:123
};
let monitor=new Proxy(obj,{
// 拦截对象属性的读取
get(target,key){
return target[key].replace('2017','2018')
},
// 拦截对象设置属性
set(target,key,value){
if(key==='name'){
return target[key]=value;
}else{
return target[key];
}
},
// 拦截key in object操作
has(target,key){
if(key==='name'){
return target[key]
}else{
return false;
}
},
// 拦截delete
deleteProperty(target,key){
if(key.indexOf('_')>-1){
delete target[key];
return true;
}else{
return target[key]
}
},
// 拦截 Object.keys,Object.getOwnPropertySymbols,Object.getOwnPropertyNames
ownKeys(target){
return Object.keys(target).filter(item=>item!='time')
}
});
console.log('get',monitor.time);//get 2018-03-11
monitor.time='2018';
monitor.name='mu';
console.log('set',monitor.time,monitor);//set 2018-03-11 Proxy{time: "2017-03-11", name: "mu", _r: 123}
console.log('has','name' in monitor,'time' in monitor);//has true false time被隐藏了
console.log('ownKeys',Object.keys(monitor));//ownKeys ["name", "_r"] time被隐藏了
delete monitor._r;
console.log('delete',monitor);//Proxy{time: "2017-03-11", name: "mu"}
}
②Reflect直接用
{
let obj={
time:'2018-11-27',
name:'net',
_r:123
};
console.log(Reflect.get(obj,'time'));//2018-11-27
Reflect.set(obj,'name','muu');
console.log(obj);//{time: "2018-11-27", name: "muu", _r: 123}
console.log(Reflect.has(obj,'name'));//ture
}
十二.类
基本语法 类的继承 静态方法 静态属性 getter setter
{
// 基本定义和生成实例
class Parent{
constructor(name='muu'){
this.name=name;
}
}
let v_parent = new Parent('v');
console.log('构造函数和实例',v_parent);//构造函数和实例 Parent {name: "v"}
}
{
// 继承
class Parent{
constructor(name='muu'){
this.name=name;
}
}
class Child extends Parent{
}
console.log('继承',new Child());//继承 Child {name: "muu"}
}
{
// 继承传递参数
class Parent{
constructor(name='muu'){
this.name=name;
}
}
class Child extends Parent{
constructor(name='child'){
super(name);//一定要放在构造函数第一行
this.type='child';
}
}
console.log('继承传递参数',new Child('hello'));//继承传递参数 Child {name: "hello", type: "child"}
}
{
// getter,setter
class Parent{
constructor(name='muu'){
this.name=name;
}
get longName(){
return 'mu'+this.name
}
set longName(value){
this.name=value;
}
}
let v=new Parent();
console.log('getter',v.longName);//getter mumuu
v.longName='hello';
console.log('setter',v.longName);//setter muhello
}
{
// 静态方法
class Parent{
constructor(name='muu'){
this.name=name;
}
static tell(){
console.log('tell');
}
}
Parent.tell();//tell 通过类调用而不是通过实例调用
}
{
// 静态属性
class Parent{
constructor(name='muu'){
this.name=name;
}
static tell(){
console.log('tell');
}
}
Parent.type='test';
console.log('静态属性',Parent.type);//静态属性 test
}
十三.Promise(异步编程的解决方法,a执行完再执行b)
优点:Promise可以异步执行多个,code简洁
{
// 传统回调基本定义 打印执行,1秒后打印timeout1
let ajax=function(callback){
console.log('执行');
setTimeout(function () {
callback&&callback.call()
}, 1000);
};
ajax(function(){
console.log('timeout1');
})
}
{
//打印执行2,1秒后打印promise timeout2
let ajax=function(){
console.log('执行2');
return new Promise(function(resolve,reject){
setTimeout(function () {
resolve()
}, 1000);
})
};
ajax().then(function(){//ajax()是一个promise实例,可有很多then方法
console.log('promise','timeout2');
})
}
{
let ajax=function(num){
console.log('执行3');
return new Promise(function(resolve,reject){
if(num>5){
resolve()
}else{
throw new Error('出错了')
}
})
}
ajax(6).then(function(){//执行3 log 6
console.log('log',6);
}).catch(function(err){
console.log('catch',err);
});
ajax(3).then(function(){//执行3 catch Error: 出错了
console.log('log',3);
}).catch(function(err){
console.log('catch',err);
});
}
十四.Iterator和for...of循环
①Iterator接口:用一种办法读取不同的数据集合(数组、Object、Map、Set)
{
let arr=['hello','world'];
let map=arr[Symbol.iterator]();
console.log(map.next());//{value: "hello", done: false}
console.log(map.next());//{value: "world", done: false}
console.log(map.next());//{value: undefined, done: true}
}
{
//自定义读取
let obj={
start:[1,3,2],
end:[7,9,8],
[Symbol.iterator](){
let self=this;
let index=0;
let arr=self.start.concat(self.end);
let len=arr.length;
return {//返回一个有next方法的对象
next(){
if(index<len){
return {
value:arr[index++],
done:false
}
}else{
return {
value:arr[index++],
done:true
}
}
}
}
}
}
for(let key of obj){
console.log(key);//1 3 2 7 9 8
}
}
②for...of不断调用Iterator
{
//for...of 用法
let arr=['hello','world'];
for(let value of arr){
console.log(value);//hello world
}
}
十五.Generator(异步编程)
进入函数停在第一个yield前,执行一个next()方法,执行一个yield,直到没有可往下执行的yield或者return则返回done:true
{
// genertaor基本定义
let tell = function* (){
yield 'a';
yield 'b';
return 'c'
};
let k = tell();
console.log(k.next());//{value: "a", done: false}
console.log(k.next());//{value: "b", done: false}
console.log(k.next());//{value: "c", done: true}
console.log(k.next());//{value: undefined, done: true}
}
{
//用genertaor部署,不手写Iterator(object对象不能用for...of除非有Iterator接口)
let obj={};
obj[Symbol.iterator]=function* (){
yield 1;
yield 2;
yield 3;
}
for(let value of obj){
console.log(value);//1 2 3
}
}
{
//状态机,可以不断查找状态
let state = function* (){
while(1){
yield 'A';
yield 'B';
yield 'C';
}
}
let status=state();
console.log(status.next());//{value: "A", done: false}
console.log(status.next());//{value: "B", done: false}
console.log(status.next());//{value: "C", done: false}
console.log(status.next());//{value: "A", done: false}
console.log(status.next());//{value: "B", done: false}
}
十六.Decorator(修饰器:函数修改类的行为)
{
//只读
let readonly=function(target,name,descriptor){
descriptor.writable=false;
return descriptor
};
class Test{
@readonly
time(){
return '2018-11-27'
}
}
let test=new Test();
console.log(test.time());//2018-11-27
test.time=function(){
console.log('reset time');
};
console.log(test.time());//2018-11-27
}
{
let typename=function(target,name,descriptor){
target.myname='hello';
}
@typename//可以放class上面
class Test{
}
console.log('类修饰符',Test.myname);//类修饰符 hello
//可引入第三方库修饰器的js库,不用自己写,直接@就可以:core-decorators; npm install core-decorators
}
十七.模块化
export let A=123;
export function test(){
console.log('test');
}
export class Hello{
test(){
console.log('class');
}
}
import {A,test,Hello} from './xx';//{}中取什么写什么,./xx是上面内容的路径
import *as lesson1 from './xx';//取所有内容放到对象lesson1中
-------------------------------------------
let A=123;
let test=function(){
console.log('test');
}
class Hello{
test(){
console.log('class');
}
}
export default {A,test,Hello}//导出,但是我不指定名字,路径为./xxx
import lesson from './xxx';//我随便叫lesson,它接收了上面的内容
console.log(lesson.A);//
ECMAScript6语法重点(二)的更多相关文章
- ECMAScript6语法重点(一)
一. let和const ①let声明的变量只在它的块作用域有效({ }括起来) ②let不能重复声明同一变量 ③const声明的常量不能改(但对象可以加属性) ④const也有块作用域概念 ⑤con ...
- ECMAScript6 语法
声明变量 var let const var(声明局部变量,有局部外可用缺陷) 代码 <!DOCTYPE html> <html lang="en"> &l ...
- CSS基本语法(二)
目录 CSS基本语法(二) 八.CSS复合选择器 1.后代选择器** 2.子选择器 3.并集选择器** 4.伪类选择器 链接伪类选择器 :focus伪类选择器 总结 九.CSS的元素显示样式 1.概念 ...
- Swift3.0基础语法学习<二>
对象和类: // // ViewController2.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/15. // Copyright © ...
- java基础语法(二)--单列模式
java基础语法(二)--单列模式 /** * 功能:单列模式 * @author Administrator * */ public class SingletonTest { public sta ...
- Ruby语法基础(二)
Ruby语法基础(二) 继续ruby的学习,这次主要把目光放到运算符,条件判断,循环,方法,以及其他ruby特有的基本概念上 运算符 算术运算符:+,-,/,%,**,值的注意的是,ruby中一切皆为 ...
- R语言语法基础二
R语言语法基础二 重塑数据 增加行和列 # 创建向量 city = c("Tampa","Seattle","Hartford"," ...
- JS基础语法(二)
目录 JavaScript基础语法(二) 八. 函数 1. 函数的概念 2. 函数的使用 声明函数 调用函数 3. 函数的封装 4. 函数的参数 函数的参数匹配问题 5. 函数返回值 6. argum ...
- Golang 基础之基础语法梳理 (二)
大家好,今天将梳理出的 Go语言基础语法内容,分享给大家. 请多多指教,谢谢. 本次<Go语言基础语法内容>共分为三个章节,本文为第二章节 Golang 基础之基础语法梳理 (一) Gol ...
随机推荐
- いろはちゃんとマス目 / Iroha and a Grid (组合数学)
题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_b Time limit : 2sec / Memory limit : 256MB Score ...
- flask框架----flask入门
一.Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是 ...
- 前端框架VUE----指令
一.什么是VUE? 它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 二.怎么使用VUE? 1.引入vue.js 2.展示HTML <div id=" ...
- Django框架----模板语法
Django模板系统 官方文档 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 只需要记两种特殊符号: {{ }}和 {% %} 变量 ...
- 前端页面报net::ERR_CONNECTION_RESET错误的原因
本机和测试环境都是OK的.但是一到线上就报错:可能原因总结如下: 1 可能是服务器限制了文件上传的权限. 解决方法:开通了文件上传权限. 2 也许导致这种错误的方式有很多,可能是因为post请求时提交 ...
- webform 使用富文本编辑器
<div class="form-group"> <label class="col-xs-2 control-label text-right&quo ...
- windows composer 安装,使用新手入门
一.前期准备: 1.下载安装包,https://getcomposer.org/download/ 2.在php.ini文档中打开extension=php_openssl.dll 3.下载php_s ...
- MD5与SHA散列单项加密
MD5 MD5的英文全称是Message Digest Algorithm MD5,译为消息摘要算法第五版,是众多哈希算法中的一种(哈希算法是一种可以将任意长度的输入转化为固定长度输出的算法).因此M ...
- Linux Redhat 安装免费yum源
Linux Redhat 安装免费yum源 出处地址:http://www.cnblogs.com/nbartchen/p/8565816.html 1.查看是否安装相关包 rpm -qa|grep ...
- .net 高并发 多消费者模式处理订单
1.数据直接往rabbitmq抛或者判断redis里面是否有订单2.多消费者模式往数据库刷单3.判断redis是否有这个订单了,没有的话插入订单,存在了的话,不插入订单4.检索出订单,然后往队列分发到 ...