NodeJS设计模式(一)

NodeJS最新的主要版本针对ES2015(也称为ES6)新功能特性进行了大量的补充

//let关键字,块级作用域
if(false){
    var x = "hello";
}
console.log(x);

//const定义常量,给常量赋值报错
const numbers=[2,5,6,7,8,9,0,1];
const even = numbers.filter(x=>{
    if(x%2===0){
        console.log(x+' is even! ');
        return true;
    }
})

箭头函数
1、自动调用return
2、绑定自身作用域

//过去语法
function Person(name,surname,age){
    this.name = name;
    this.surname = surname;
    this.age = age;
}
Person.prototype.getFullName =function(){
    return this.name + ''+this.surname;
};
Person.older = function(person1,person2){
    return (person1.age>=person2.age)?person1:person2;
}

//新语法之类语法

class Person{
constructor(name,surname,age){
    this.name = name;
    this.surname = surname;
    this.age = age;
}
getFullName(){
    return this.name+''+this.surname;
}
static older (person1,person2){
    (person1.age>=person2.age)?peroson1:person2;
}
}

extend 和super 关键字来扩展Person属性的可能性
继承更为方便,声明了一个想要扩展的类,定义了一个新的构造函数,它可以使用关键字super调用父类,并重写了getFullName方法以添加对中间名的支持

class PersonWithMiddlename extends Person {
	constructor (name , middlename, surname, age) {
		super(name, surname, age)
		this.middlename = middlename
	}
	getFullName() {
		return this.name +''+ this.middlename + ''+this.surname
	}
}

三、get 和 set

const person ={
	name:'George',
	surname: 'Boole',
	get fullname(){
		return this.name +'' +this.surname
	},
	set fullname(){
		let parts = fullname.split('')
		this.name = parts[0]
		this.surname = parts[1]
	}
}
console.log(person.fullname)
console.log(person.fullname= 'Alan Turing' )
console.log(person.name)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值