JS 一个封装插件的模板

function mapilotSdk(options){
    var beanA = {"name":"A","value":1}
    var beanB = null;
	
    function MapilotSDK(options) {
        var default_options = {
            "O":"o"
        };
		this.options = _mergeOptions(default_options, options)
		beanB = {"name":"B","value":2}
    };
    MapilotSDK.prototype = {
        printA: function(){
            console.log(beanA);
        },
		changeA: function(val){
			beanA.value = val;
		},
		printB: function(){
            console.log(beanB);
        },
		changeB: function(val){
			beanB.value = val;
		},
		printOptions: function(){
			console.log(this.options)
		},
		changeOptions: function(key, val){
			this.options[key]=val;
		}
    };
	
	function _mergeOptions(obj1, obj2) {
		var obj3 = {};
		for (var attrname in obj1) {
			obj3[attrname] = obj1[attrname];
		}
		for (var attrname in obj2) {
			obj3[attrname] = obj2[attrname];
		}
		return obj3;
	}

    var instance = function (options) {
        return new MapilotSDK(options);
    };
    return instance(options)
}

  

posted @ 2020-10-02 11:04  413Xiaol  阅读(128)  评论(0编辑  收藏  举报