ES6记录
let //局部变量 const //声明变量,但是声明的是常量。一旦声明,常量的值就不能改变。
let
局部声明变量,不存在变量提升,不允许重复声明,块级作用域
const命令
const也用来声明变量,但是声明的是常量。一旦声明,常量的值就不能改变。
跨模块常量
// constants.js 模块
export const A = 1;
export const B = 3;
export const C = 4;
// test1.js 模块
import * as constants from './constants';
console.log(constants.A); // 1
console.log(constants.B); // 3
// test2.js 模块
import {A, B} from './constants';
console.log(A); // 1
console.log(B); // 3
类(class)
class Animal {
// 构造方法,实例化的时候将会被调用,如果不指定,那么会有一个不带参数的默认构造函数.
constructor(name,color) {
this.name = name;
this.color = color;
}
// toString 是原型对象上的属性
toString() {
console.log('name:' + this.name + ',color:' + this.color);
}
}
var animal = new Animal('dog','white');
animal.toString();
console.log(animal.hasOwnProperty('name')); //true
console.log(animal.hasOwnProperty('toString')); // false
console.log(animal.__proto__.hasOwnProperty('toString')); // true
class Cat extends Animal {
constructor(action) {
// 子类必须要在constructor中指定super 方法,否则在新建实例的时候会报错.
// 如果没有置顶consructor,默认带super方法的constructor将会被添加、
super('cat','white');
this.action = action;
}
toString() {
console.log(super.toString());
}
}
var cat = new Cat('catch')
cat.toString();
// 实例cat 是 Cat 和 Animal 的实例,和Es5完全一致。
console.log(cat instanceof Cat); // true
console.log(cat instanceof Animal); // true
Module
//test.js
var name = 'Rainbow';
var age = '24';
export {name, age};
//index.js
import {name, age} from './test.js'
整体输入,module指令
export function getName() {
return name;
}
export function getAge(){
return age;
}
import * as test form './test.js';
module test from 'test.js';
test.getName();
export default
// default 导出
export default function getAge() {}
// 或者写成
function getAge() {}
export default getAge;
// 导入的时候不需要花括号
import test from './test.js';
浙公网安备 33010602011771号