nodejs之koa学习笔记(1)

本文详细介绍了ECMAScript 6 (ES6) 的关键新特性,包括类(class)的使用方法、生成器(generator)的基本写法及应用、Symbol类型的创建与使用等。此外,还深入探讨了类的构造函数、继承机制、静态成员函数、类型数组等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新特性
1. classes - 各种 ‘类’,再也无需用 CoffeeScript 的语法糖写类了
2. typed arrays - 类型数组
3. generators - 未来的.js 代码中将有无数生成器,不学一点就看不懂 JS 代码了哦
4. collections - 集合、映射、弱集合、弱映射
5. arrow functions - 箭向函数
6. block scoping - 使用 let 、const 作用域,块辖域
7. template strings - 模板字串
8. promises - 用标准化了的方法进行延迟和异步计算
9. symbols - 唯一的、不可修改的数据

classes

先声明后使用,未知可随意

 class Qinzhao{

}
或者
var Qinzhao = class{

}

构造函数

1 var Ptthon = class{              
  2         constructor(height,width){   
  3                 this.height = height;
  4                 this.width=width;
  5         }
  6                                                                                                                                                          
  7 } 

set以及get的方法

var Ptthon = class{
  2         constructor(height,width){                                                                                                                       
  3                 this.height = height;
  4                 this.width=width;
  5         }
  6         
  7         
  8         get name(){
  9                 return this.name;
 10         }
 11         
 12         set name(somename){
 13         
 14                 this.name= somename;
 15         }
 16         
 17         
 18         get area(){
 19         
 20                 return this.height*this.width;
 21         }
 22 }       

继承extends:

class Animal{
  2        
  3         constructor(name){
  4                 this.name = name;
  5         }
  6        
  7        
  8         say(){
  9         
 10         console.log(this.name+" try to say something...");
 11         }
 12 }      
 13        
 14 class Dog extends Animal{
 15        
 16         say(){
 17                 super.say();
 18                 console.log(this.name+"barking...");
 19         }
 20 }
 21  
 22  
 23 var dog = new Dog;
 24 dog.name="dog ";
 25  
 26 console.log(dog.say());

静态成员函数

class Point(){
  constructor(x, y){
      this.x = x;
      this.y = y;
  }
  static distance(a, b){
      constant dx = a.x - b.x;
      constant dy = a.y - b.y;
      return Math.sqrt(dx * dx + dy * dy);
  }
}

Generator的基本写法

function* helloworld(){
    yield "Hello";
    return "World!";
}

func = helloworld();
func.next();//return { value: 'Hello', done: false }
func.next();//return { value: 'World!', done: true }
func.next();//return { value: '', done: true }
next方法参数

function* f(){                                                                                                                                           
  2         for (var i = 0;true;i++){
  3                 var reset = yield i;
  4                 if(reset){
  5                         i = -1;
  6                 }
  7         }
  8  
  9 }
 10  
 11 var g = f();
 12  
 13  
 14 console.log(g.next());
 15 console.log(g.next());
 16 console.log(g.next());
 17 console.log(g.next());
 18 console.log(g.next());
 19 console.log(g.next());
 20 console.log(g.next());
 21 console.log(g.next());
 22 console.log(g.next());
 23 console.log(g.next());
 24 console.log(g.next());
 25 console.log(g.next());
 26 console.log(g.next());
 27 console.log(g.next());

for…of循环

function* loopThroughInt(){
    for (let index = 0; index < 100; index++){
        yield index;
    }
    return 100;
}
for (let v of loopThroughInt()){
    console.log(v); //output 0...99
}

如果在Generator函数内部需要调用另外一个Generator函数,那么对目标函数的调用就需要使用yield*,以下是一个简单的例子

function* objects(){
  2         yield "cat";
  3         yield "dog";
  4         yield "duck";
  5  
  6  
  7  
  8 }
  9        
 10        
 11 function* say(){
 12        
 13         yield* objects();
 14         yield " say node js";
 15 }      
 16        
 17 var f =say();
 18        
 19 console.log(f.next());
 20 console.log(f.next());
 21 console.log(f.next());
 22 console.log(f.next());
 23 console.log(f.next());
 { value: 'cat', done: false }
{ value: 'dog', done: false }
{ value: 'duck', done: false }
{ value: ' say node js', done: false }
{ value: undefined, done: true }

创建Symbol实例

# 第一种方法直接使用Symbol构造函数
var s1 = Symbol();
# 为创建的Symbol对象指定一个名称
var s2 = Symbol("test");
# 使用这种方法,每次创建出来的Symbol对象都是不一样的
# console.log(s2==s3)将返回false
var s3 = Symbol("test");

# 第二种方法,使用Symbol.for方法来创建Symbol实例
# 使用Symbol.for方法来创建一个Symbol实例的时候,
# 系统首先会在一个全局的注册表中查找是否有相同Key名称的Symbol被创建了,如果找到,就返回已经存在的对象
# 否则,将创建一个全新的对象
s1 = Symbol.for("test")
s2 = Symbol.for("test")
console.log(s1 == s2) //will return true
console.log(s1 == s3) // will return false,因为s3是通过Symbol调用产生的

因为Symbol对象不是一个字符串,所以原先的Object.getOwnPropertyNames()方法并不会返回它们,你需要使用专门的Object.getOwnPropertySymbols()方法来访问它们。

ar a = {};
var s1 = Symbol("test");
var s2 = Symbol("testfunc");
a[s1] = "hello world!";
a[s2] = function(){
    console.log("Test function");
};
//below code will return []
console.log(Object.getOwnPropertyNames(a));
//below code will return [Symbol(test), Symbol(testfunc)]
console.log(Object.getOwnPropertySymbols(a));

引用实例2 — 消除魔术字符串

# 引入Symbol前的代码
# 在这个代码中es, ch就是魔术字符串
# 当你在调用这个函数的时候,必须确保输入的参数是一个有效的魔术值
function getHello(country){
    switch(country){
        case "es":
            return "Holla";
        case "ch":
            return "你好";
        default:
            return "Hello";
    }
}
console.log(getHello("es"));
console.log(getHello("ch"));
# 引入Symbol之后的代码
# 在这个代码中es, ch不再是一个具体的字符串
# 因此不管在COUNTRY_CODE中如何修改es, ch的定义,调用方的代码都不需要修改
const COUNTRY_CODE= {
    es: Symbol(),
    ch: Symbol()
}
function getHello(country){
    switch(country){
        case COUNTRY_CODE.es:
            return "Holla";
        case COUNTRY_CODE.ch:
            return "你好"
        default:
            return "Hello"
    }
}
console.log(getHello(COUNTRY_CODE.es));
console.log(getHello(COUNTRY_CODE.ch));

Symbol.unscopables

# when Test has no Symbol.unscopables
class Test{
saySomething() { return "abc"; }
}
var saySomething = function() { return "123"; }
with(Test.prototype){
saySomething(); // return "abc"
}
# when Test has Symbole.unscopables
class Test{
saySomething() { return "abc"; }
get [Symbol.unscopables] {
    return {saySomething: true};
}
}
with(Test.prototype){
saySomething(); // now return "123"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值