用Java合并对象的本机方法
Javascript的对象没有任何本机合并操作.如果您有两个物体,请说
Javascript's Object doesn't have any native merge operation. If you have two objects, say
{a:1, b:2}
{c:3, d:4}
想要得到
{a:1, b:2, c:3, d:4}
据我所知,您必须遍历对象.也就是说,您决定采用左合并或右合并策略,然后执行类似(简化)的操作
As far as I know, you have to iterate through the objects. That is to say that you decide on either a merge left or merge right strategy and then you do something like (simplified)
for (key in object2) {
object1[key] = object2[key];
}
这很好.但是,Javascript具有call
和prototype
功能.例如,可以使用
This is fine. However, Javascript has the call
and prototype
feature. For instance, turning arguments
into an Array
can be done with
Array.prototype.slice.call(arguments)
这种方法利用了现有的本机代码,因此不易受程序员的愚蠢影响,并且比非本机实现要快.
This approach exploits existing native code, and so therefore is less susceptible to programmer folly and should run faster than a non-native implementation.
是否有技巧在DOM的Attribute
或Node
遍历功能或某些通用的String
函数上使用此原型/调用模式,以便进行本机对象合并?
Is there a trick to use this prototype/call pattern on perhaps the Attribute
or Node
traversal features of the DOM, or perhaps some of the generic String
functions in order to do a native object merge?
代码看起来像这样:
var merged = somethingrandom.obscuremethod.call(object1, object2)
结果,您将获得本机合并而无需遍历.
And as a result, you'd get a native merge without a traversal.
如果可以使用Object
的constructor
属性,然后强制一个对象具有另一个对象的构造函数,然后对复合对象运行new
,则可以免费获得合并.但是我不完全了解javascript中constructor
功能进行此调用的全部含义.
If you could use the constructor
property of an Object
and then coerce one object to have a constructor of another object and then run new
over the composite object, you may get a merge for free. But I don't have a firm grasp of the full implications of the constructor
feature in javascript to make this call.
对于Arrays
,同样的问题也成立.一个常见的问题是取7个数字数组,然后尝试找出这些数组的交集.也就是说,所有7个数组中都存在哪些数字.
The same question holds true for Arrays
. A common problem is to take, say 7 arrays of numbers, then try to find out the intersection of those arrays. That is to say, which numbers exist in all 7 arrays.
您可以将它们连接在一起,然后进行排序,然后遍历.但是,如果有一个通用的相交隐藏在某个地方,我们可以强迫数组进行本机处理,那就太好了.
You could concat them together, then do a sort, and then do a traversal, surely. But it would be nice if there is a generic intersect tucked away somewhere that we can coerce an array to doing natively.
有什么想法吗?
对于阵列问题,您可以执行以下操作:
For the array problem, you could do the following:
array.concat(a,b,c).sort().join(':'),然后使用一些棘手的RegExp
捕获并重复模式以进行遍历. RegExp实现(如果您不知道)可以在非常简单的基于堆栈的虚拟机上运行.当您初始化正则表达式时,实际上是一个要编译的程序(RegExp.compile是已弃用的JS方法).然后,本地人以极快的速度在字符串上运行.也许您可以利用它来获得成员资格阈值并获得更好的性能...
array.concat(a, b, c).sort().join(':') and then use some tricky RegExp
capture and repeat patterns in order to traverse. RegExp implementations, if you don't know, run on a very simple stack-based virtual machine. When you initialize your regular expression that's really a program that gets compiled (RegExp.compile is a deprecated JS method). Then the native runs over the string in a blisteringly fast way. Perhaps you could exploit that for membership thresholds and get better performance...
它仍然不能一路走来.
我对此的回答令人失望,但仍然:
My answer to this will be disappointing, but still:
否
这样做的原因很简单:Resig先生在jQuery中实现合并(或称为扩展",因为它称为对象)正在执行一个循环,就像您要提问的循环一样.您可以在此处进行查看.而且我敢说,如果John Resig尚未找到一种聪明的内置方法来做到这一点,那么仅仅stackoverflow的凡人也不会:)
The reason for this is simple: Mr Resig's implementation of merge (or "extend" as it's called for objects) in jQuery is doing a loop, just like the one in your question. You can look at it here. And I dare say that if John Resig hasn't found a clever build-in way to do it, then the mere mortals of stackoverflow won't either :)