小编Ale*_*lex的帖子

如何使用es6 js类表示法自动增加id值?

我在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)

javascript class ecmascript-6

3
推荐指数
1
解决办法
2657
查看次数

标签 统计

class ×1

ecmascript-6 ×1

javascript ×1