I have certain graph structure in my JS program where each node has specific functions associated to it.
Graph Structure
var g = {
"alpha_1" : getNode("alpha"),
"beta_1" : getNode("beta")
....
}
getNode() function
var getNode = function (type) {
var obj = {
'metaType': type,
'config': Object,
'data': {
'loc': {'x': '', 'y': ''},
'args': {'keys': [], 'values': []},
'return': []
},
'in': [],
'true': [],
'false': [],
'inLineId': [],
'outLineTrueId': [],
'outLineFalseId': []
//'prototype': Object.prototype
};
switch (type) {
case 'alpha':
obj.data.args.keys.push('dataStore', 'filters', 'limit', 'data');
obj.data.args.values['dataStore'] = '';
obj.data.args.values['limit'] = 'FALSE';
obj.data.args.values['data'] = 'FALSE';
obj.data.args.values['filters'] = [];
/**
* @param valueObj :{} JSON Object with fields from, value, type
*/
obj.config.defineProperty(Object.prototype, 'setDatastore', {
value: function (valueObj) {
obj.data.args.values['dataStore'] = valueObj;
},
enumerable: false,
configurable: true,
});
/**
* @param valuesArray :[] Array with fields from, value, type
*/
obj.config.defineProperty(Object.prototype, 'setReturnValues', {
value: function (valueArray) {
obj.data.args.values['return'].push.apply([], valueArray);
},
enumerable: false,
configurable: true,
});
case 'beta':
/**
* @param key
* @param op =/>=/!=/<=/</>
* @param valueObj :{} JSON Object with fields from, value, type
* @param next
*/
obj.config.defineProperty(Object.prototype, 'addFilter', {
value: function (key, op, valueObj, next) {
obj.data.args.values['filters'].push(
{
'key': key,
'op': op,
'value': valueObj,
'next': next
}
);
},
configurable: true,
enumerable: false
});
..
}
return obj;
}
Then I tried to access the defined functions in the following manner,
g.alpha.config.setDatastore({"a":"b"});
but it gives me an error.
Uncaught TypeError: g.alpha_1.config.setDatastore is not a function(…)
Can anyone help me to fix this?
obj.config.defineProperty(Object.prototype, 'setDatastore'console.log(g.alpha)and see what you get.Object {metaType: "alpha", data: Object, in: Array[0], true: Array[0], false: Array[0], config: function Object() …}'config': Objectto'config': new Object()in yourgetNode()functionUncaught TypeError: obj.config.defineProperty is not a function