我在es6中的类有一些问题.每次我创建对象时,我都需要自动增量id值.真的不明白我怎么能声明变量,给_id赋值然后增加增量变量.
class Rectangle {
constructor(name,width,height,x,y) {
if (typeof(name) === 'string' && typeof(width,height,x,y) === 'number' ) {
this._id = ?;
this._name = name;
this._width = width;
this._height = height;
this._x = x;
this._y = y;
var div = document.createElement("div");
document.body.appendChild(div);
div.id = this._id;
div.style.width = this._width + "px";
div.style.height = this._height + "px";
div.style.backgroundColor = "#ededed";
div.innerHTML = name;
}
else {
alert("No way!");
return false;
}
}
moveObj(dx,dy) {
this._x = this._x + dx
this._y = this._y + dy
console.log(this._x,this._y) …Run Code Online (Sandbox Code Playgroud)