The Javascript object has no merge operation. If you have two objects, say
{a:1, b:2} {c:3, d:4}
And I want to get
{a:1, b:2, c:3, d:4}
As far as I know, you need to iterate over objects. That is, you decide either a left merge strategy or a merge strategy, and then you do something like (simplified)
for (key in object2) { object1[key] = object2[key]; }
This is normal. However, Javascript has a call and prototype function. For example, turning arguments into Array can be done using
Array.prototype.slice.call(arguments)
This approach uses existing native code, so it is less susceptible to a stupid programmer and should work faster than a non-native implementation.
Question
Is there a trick to using this prototype / call pattern, perhaps for the Attribute or Node traversal functions of the DOM, or perhaps some of the common String functions to merge your own objects?
The code looks something like this:
var merged = somethingrandom.obscuremethod.call(object1, object2)
And as a result, you will get your own merge without going around.
Possible suboptimal solution
If you can use the constructor property for Object , and then force one object to create the constructor of another object, and then run new on the compound object, you can get the merge for free. But I do not have a clear understanding of all the consequences of the constructor function in javascript to make this call.
Lemma
The same question holds true for Arrays . A common problem is to take, for example, 7 arrays of numbers, and then try to figure out the intersection of these arrays. That is, what numbers exist in all 7 arrays.
You could combine them together, then pretend, and then make a detour, of course. But it would be nice if some general intersection were crossed somewhere, which we can force the array to execute initially.
Any thoughts?
edit:
Halfway there
For an array problem, you can do the following:
array.concat (a, b, c) .sort (). join (':') and then use some complex RegExp capture and replay patterns to go through. The RegExp implementation, if you do not know, runs on a very simple stack-based virtual machine. When you initialize your regular expression, which is really a compilation of the program (RegExp.compile is an outdated JS method). Then the native runs the line quickly. Perhaps you can use this for membership thresholds and get better performance ...
He still does not go all the way.