简体   繁体   English

我需要将此代码转换为ES6,我在JS上是一个新手,不确定如何

[英]I need to convert this code to ES6, I'm very new on JS and I'm not sure how to

I'm new on JS and not very familiar with the old version, I have this test that I need to convert into Es6. 我是JS的新手,对旧版本不是很熟悉,我需要将此测试转换为Es6。 Does anyone could help me to understand how I can do this? 有谁能帮助我了解我该怎么做?

'use strict';
function Shape(id, x, y) {
 this.id = id;
 this.setLocation(x, y);
}
Shape.prototype.setLocation = function(x, y) {
 this.x = x;
 this.y = y;
};
Shape.prototype.getLocation = function() {
 return {
 x: this.x,
 y: this.y
 };
};
Shape.prototype.toString = function() {
 return 'Shape(' + this.id + ')';
};
function Circle(id, x, y, radius) {
 Shape.call(this, id, x, y);
 this.radius = radius;
}
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;
Circle.prototype.toString = function() {
 return 'Circle > ' + Shape.prototype.toString.call(this);
};

Converted to classes : 转换为

 class Shape { constructor(id, x, y) { this.id = id; this.setLocation(x, y); } setLocation(x, y) { this.x = x; this.y = y; } getLocation = () => ({ x: this.x, y: this.y }) toString() { return `Shape(${this.id})`; } } class Circle extends Shape { constructor(id, x, y, radius) { super(id, x, y) this.radius = radius; } toString() { return `Circle> ${super.toString()}`; } } let shape = new Shape('shape', 5, 5); let circle = new Circle('circle', 6, 6, 6); console.log("Shape()", shape.getLocation(), shape.toString() ); console.log("Circle()", circle.getLocation(), circle.toString() ); 

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM