class声明会提升,和let,const一样存在暂时性死区
let ani = new Animal()
function Animal() {
this.name = 'le'
}
let dog = new Dog()
class Dog {
}
class声明内部会启用严格模式
function Animal() {
this.name = 'le'
age = 10
}
const ani = new Animal()
class Dog {
constructor() {
sex = 1
}
}
let dog = new Dog()
必须使用new来调用class
function Animal() {
this.name = 'le'
console.log('animal')
}
const ani = Animal()
class Dog {
constructor() {
this.name = 'le'
}
}
let dog = Dog()
class的静态方法和原型方法都是不可枚举的
function Animal() {
this.name = 'le';
};
Animal.static_run = function() {
console.log('animal static run');
}
Animal.prototype.say = function() {
console.log('animal say');
}
Animal.prototype.eat = function() {
console.log('animal eat');
}
const ani = new Animal();
class Dog {
constructor() {
this.name = 'le';
}
static static_run() {
console.log('dog static run');
}
say() {
console.log('dog say');
}
eat() {
console.log('dog eat');
}
}
let dog = new Dog();
console.log('Animal 静态方法:', Object.keys(Animal));
console.log('Animal 原型方法:', Object.keys(Animal.prototype));
console.log('Dog 静态方法:', Object.keys(Dog));
console.log('Dog 原型方法:', Object.keys(Dog.prototype));
class的静态方法和原型方法都没有prototype,不能使用new来调用
function Animal() {
this.name = 'le';
};
Animal.static_run = function() {
console.log('animal static run');
}
Animal.prototype.say = function() {
console.log('animal say');
}
Animal.prototype.eat = function() {
console.log('animal eat');
}
const ani = new Animal();
class Dog {
constructor() {
this.name = 'le';
}
static static_run() {
console.log('dog static run');
}
say() {
console.log('dog say');
}
eat() {
console.log('dog eat');
}
}
let dog = new Dog();
console.log('Animal 静态方法static_run属性:', Reflect.ownKeys(Animal.static_run));
console.log('Animal 原型方法say属性:', Reflect.ownKeys(Animal.prototype.say));
console.log('Dog 静态方法static_run属性:', Reflect.ownKeys(Dog.static_run));
console.log('Dog 原型方法say属性:', Reflect.ownKeys(Dog.prototype.say));
const obj_say = new Dog.prototype.say();
class 子类的__proto__等于父类,es5中子类的__proto__为Function.prototype
function People() {
this.name = 'le'
}
function Chinese() {
People.call(this)
}
Chinese.prototype = Object.create(People.prototype)
Chinese.prototype.constructor = Chinese
class Animal {
constructor() {
this.name = 'le'
}
}
class Dog extends Animal {}
console.log(Chinese.__proto__ === People)
console.log(Chinese.__proto__ === Function.prototype)
console.log(Dog.__proto__ === Animal)
console.log(Animal.__proto__ === Function.prototype)
ES5继承是先新建子类的实例对象this,再将父类的属性添加到子类上,ES6是先新建父类的实例对象this,然后再用子类的构造函数修饰this,使得父类的所有行为都可以继承
function ES5Array(len) {
Array.call(this, len);
}
ES5Array.prototype = Object.create(Array.prototype);
ES5Array.prototype.constructor = ES5Array;
class ES6Array extends Array {
constructor(len) {
super(len);
}
};
const arr5 = new ES5Array(3);
const arr6 = new ES6Array(3);
console.log(arr5.length);
console.log(arr6.length);