如何区别ES5和ES6创建类(异同点)

1、ES5创建类

function Point(x,y,z){
	this.x = x;
	this.y = y;
	this.z = z;
}

Point.prototype.toString = function(){
	return '(' + this.x + ',' + this.y + ',' + this.z + ')';
}

var p = new Point(23,67,98);
console.log(p,typeof p,typeof Point,Point === Point.prototype.constructor);

2、ES6创建类

class Point{
	constructor(x,y,z) {
	    this.x = x;
	    this.y = y;
	    this.z = z;
	}
	
	toString(){
		return '(' + this.x + ',' + this.y + ',' + this.z + ')';
	}
}

let p = new Point(89,56,12);
console.log(p,typeof p,typeof Point,Point === Point.prototype.constructor);

3、运行结果

Point { x: 23, y: 67, z: 98 } 'object' 'function' true

 

你可能感兴趣的:(JavaScript)