猿问

关于js类的封装 实例化 参数问题

提问:我用JS封装了一个类,封装方式

var dragbox = function (config, returntype) {

    var dom = this.init(config);

    // return dom;

} //构造函数

classtest.prototype={……},

为什么这个类的实例化参数会有公用问题?

就是我第一实例化的参数 ,会沿用到第二个实例化的参数,做对比

就是我类中有个辅助函数


jsonclone: function (old_object, new_object) {

        for (var key in new_object) {

            if (typeof old_object[key] != typeof new_object[key]) {

                console.warn(key + "类型错误");

                return;

            } else if (typeof old_object[key] == "object") {

                this.jsonclone(old_object[key], new_object[key]);

            } else if (old_object[key] != undefined) {

                old_object[key] = new_object[key];

            }

        }

     }

init:function (config) {

    if (config == undefined || config == null) {

        console.log("未初始化");

        return;

    }

    var othis = this;

    this.jsonclone(this.config, config);

    this.dragdom = this.init_create();

    // return this.dragdom;

}

这个类在第二次实例化的时候,类的config里参数变成第一次实例化的参数,这是为什么??

是我构建类的方法不对么?


长风秋雁
浏览 423回答 1
1回答

GCT1015

实例化的 new 一下var config1 = {};var config2 = {};var demo1 = new dragbox(config1, null);var demo2 = new dragbox(config2, null);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答