2013-04-18 61 views

回答

0

有沒有內置的方式來做到這一點,只有自己的功能。

我建議這樣的代碼:

public static function diff(obj1:Object, obj2:Object):Object 
{ 
    if(!obj1 || !obj2) 
     return null; 

    var diffObj:Object = {}; 
    for(var key:String in obj1) 
    { 
     if(key in obj2) 
     { 
      diffObj[key] = obj1[key] - obj2[key]; 
     } 
    } 

    return diffObj; 
} 

diff({prop1:1, prop2:2}, {prop2:2, prop1:3}) 
//output: 
[object Object]: 
    prop2:int = 0 
    prop1:int = -2 
相關問題