繁体   English   中英

Javascript:“扩展”没有实例的对象

[英]Javascript : “extend” an object without an instance

我遇到的情况是我有一个大而复杂的对象,我需要在其他地方稍加修改(扩展属性等)来重复使用它

为了提高简单性和可重用性,我认为我会尝试“扩展”原始对象。 所以看起来像这样...

(function ($, window) {
    website.module('admin.models', function (exports, require) {
        exports.item = {
                    // large, complex object that will be instantiated by other things
            };
    });
})(jQuery, window);

正在使用的库是Telerik的Kendo UI ,而我正在使用其MVVM系统。 item函数实际上是kendo.data.Model.define一个实例,该实例正在获得模板。 Kendo.data.Model.define的文档

所以这就像...

var item = new website.admin.models.item({
    // specific property values
});

这将创建一个可以绑定到页面的新视图模型。 基础模型具有很多行为,因此更改视图模型本身不会覆盖或更改基础核心。

这很好。 它将所需的功能移到一个单独的文件中,并清理了较大的实现。 这也是该函数的预期用法,我正在使用的库的文档中对此进行了概述。

但是,还有另一个名为schema模型,除了具有一些额外的属性外,它与item基本上相同。

与其复制并粘贴项目模型,不如尝试扩展它会更聪明。 所以我尝试了这种方法

(function ($, window) {
    website.module('admin.models', function (exports, require) {
        exports.schema = $.extend({
               Item: {
                  Id: null, // will be filled in on the UI
                  Name: null, // will be filled in on the UI
                  Description: null, // will be filled in on the UI
                  Timestamp: null // will be filled in on the UI
               },
               Editing: false
        }, website.admin.models.item);
    });
})(jQuery, window);

但是,这不起作用,因为给定的路径不是“ item”的实例。 但是我被明确告知,我也不应该在这里使用“ new”运算符。 我只是想从项目中“拉”实际模型,然后进行扩展。

有什么办法吗? 还是这超出了Javascript的能力?

更新

用于“命名空间”的代码是这样;

(function (global) {
    var globalNamespace = global['website'];
    var VERSION = '3.0.1';

    function Module() { }

    function numeric(s) {
        if (!s) {
            return 0;
        }
        var a = s.split('.');
        return 10000 * parseInt(a[0]) + 100 * parseInt(a[1]) + parseInt(a[2]);
    }

    if (globalNamespace) {
        if (numeric(VERSION) <= numeric(globalNamespace['VERSION'])) {
            return;
        }
        Module = globalNamespace.constructor;
    } else {
        global['website'] = globalNamespace = new Module();
    }
    globalNamespace['VERSION'] = VERSION;

    function require(path) {
        path = path.replace(/-/g, '_');
        var parts = path.split('.');
        var ns = globalNamespace;
        for (var i = 0; i < parts.length; i++) {
            if (ns[parts[i]] === undefined) {
                ns[parts[i]] = new Module();
            }
            ns = ns[parts[i]];
        }
        return ns;
    }

    var proto = Module.prototype;

    proto['module'] = function (path, closure) {
        var exports = require(path);
        if (closure) {
            closure(exports, require);
        }
        return exports;
    };

    proto['extend'] = function (exports) {
        for (var sym in exports) {
            if (exports.hasOwnProperty(sym)) {
                this[sym] = exports[sym];
            }
        }
    };
}(this));

由于您的对象包含嵌套对象,因此需要使用深层扩展。 为此,将true作为第一个参数添加到$.extend

exports.schema = $.extend(true, {
   Item: {
      Id: null, // will be filled in on the UI
      Name: null, // will be filled in on the UI
      Description: null, // will be filled in on the UI
      Timestamp: null // will be filled in on the UI
   },
   Editing: false
}, website.admin.models.item);

http://jsfiddle.net/2Jw9L/

当您添加kendoui时,以这种方式扩展它变得很困难(我不会说不可能,可能有一种我没有想到的方法)来扩展它,并且更容易在模块外部创建对象并扩展它。

var theObj = {
   ...
};

(function ($, window, kendo) {
    ehrpg.module('admin.models', function (exports, require) {
        exports.item = kendo.data.Model.define($.extend(true,{},theObj));
    });
})(jQuery, window, kendo);

(function ($, window, kendo) {
    ehrpg.module('admin.models', function (exports, require) {
        exports.schema = kendo.data.Model.define($.extend(true,{
            Item: {
                Id: null,
                Name: null,
                Dirty: false,
                Timestamp: null
            }
        },theObj));
    });
})(jQuery, window, kendo);

http://jsfiddle.net/MsrP6/9/

暂无
暂无

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

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