gpt4 book ai didi

Javascript : "extend" an object without an instance

转载 作者:行者123 更新时间:2023-11-30 05:41:41 25 4
gpt4 key购买 nike

我有一个情况,我有一个大而复杂的对象,我需要通过轻微修改(扩展属性等)在其他地方重新使用它

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

(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 实例。 <强> documentation for Kendo.data.Model.define

所以这个用法就像...

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

这将创建一个可以绑定(bind)到页面的新 View 模型。底层模型有很多行为,因此更改 View 模型本身不会有覆盖或更改底层核心的风险。

这很好用。它将我需要的功能移动到一个单独的文件中,并清理了我更大的实现。这也是该函数的预期用途,由我正在使用的库的文档概述。

但是还有另一种模型称为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/

关于Javascript : "extend" an object without an instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com