简体   繁体   English

如何基于JavaScript中的另一个对象创建属性并动态创建对象?

[英]How to create properties to and object dynamically based on another object in JavaScript?

I have a scenario where from a source object I need to create a new result object. 我有一个场景,其中需要从source对象创建一个新的result对象。

The object would need to have exactly all properties from source , with the addition of "methods", with naming based on the properties and code to be executed based on a template (in my case alert(this.x) ). 该对象将需要完全具有source所有属性,并添加“方法”,并基于属性命名并基于模板执行代码(在我的情况下是alert(this.x) )。

Note: script should keep in consideration any number of properties from source 注意:脚本应考虑来自source任何数量的属性

I would like to know: 我想知道:

  • Is it possible to do it in JS? 可以在JS中做到吗?
  • What the appropriate Technics? 什么合适的技术?
  • Any examples? 有什么例子吗?

FROM SOURCE OBJECT 从源对象

  var source = {
        a: 'a',
        b: 'b'
    };

I NEED TO GET RESULT OBJ DYNAMICALLY CREATED (BY SOME FUNCTION) 我需要动态创建结果对象(通过某些功能)

    var result = {
        a: 'a',
        b: 'b',
        _setA: function(){
            alert(this.a);
        },
        _setB: function(){
            alert(this.a);
        }
    }

Note: result is created after processing the source object 注意:结果是在处理源对象之后创建的

EDIT: 编辑:

final solution based on your answers 根据您的答案的最终解决方案

http://jsfiddle.net/kbnd6e5c/1/ http://jsfiddle.net/kbnd6e5c/1/

You can first use $.extend method to copy properties, and then you need iterate through properties and create dynamic setters. 您可以首先使用$.extend方法复制属性,然后需要遍历属性并创建动态设置器。 For example like in below code: 例如下面的代码:

var source = { a: 'a', b: 'b' };    
var result = $.extend({}, source);

Object.keys(source).forEach(function(key) {
    result['_set' + key[0].toUpperCase() + key.slice(1)] = function(value) {
        alert(this.key);
        this[key] = value;
    }
});

Check the working demo below. 查看下面的工作演示。

 var source = { a: 'a', b: 'b', test: 'test' }; var result = $.extend({}, source); Object.keys(source).forEach(function(key) { result['_set' + key[0].toUpperCase() + key.slice(1)] = function(value) { this[key] = value; } }); alert([ result.a, result.test ]); result._setA('a1'); result._setTest('test1'); alert([ result.a, result.test ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I'm not sure I have fully understanding your question but I think you should juste use that simple thing: 我不确定我是否完全理解您的问题,但我认为您应该使用这一简单的方法:

var copy = result
copy.example = {
    x: Y,
    z, T
}
copy.newFuntion = function(){...} //add your own function

This way you have everything that result have + your own things 这样,您拥有的所有结果都会拥有+您自己的东西

You can do something like this: 您可以执行以下操作:

var result = {
    a: 'A',
    b: 'B'
};

var copy = result;

Object.keys(result).forEach(function(key) {
    Object.defineProperty(copy, "_set" + key, {
        get: function() {
            return result[key]; 
        }
    })
});

copy._seta;
copy._setb;

Source 资源

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

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