试试for(var p in test1){
    if(typeof(test2[p])=="undefined")test2[p]=test1[p];
}

解决方案 »

  1.   

    <script>
    var test1={
    name:'test1',
    id: 'test2',
    size:5
    }var test2={
    name:'test2',
    id:'test2'
    }function SetValue(obj1, obj2)
    {
    for(var i in obj1)
    for(var j in obj2)
    {
    //如果属性名称相等
    if(i == j)
    {
    if(obj1[i] != obj2[j])
    obj2[j] = obj1[i];
    }
    else
    eval("obj2."+i+"= obj1[i];");
    }
    }
    SetValue(test1, test2);
    for(i in test1)
    document.write(i +":"+ test1[i]+"<br />");
    document.write("<p /><p /><p />");
    for(j in test2)
    document.write(j +":"+ test2[j]+"<br />");
    </script>
      

  2.   


    for (var Member in test2){
        if (test1[p])
             test1[p]=test2[p];
    }
      

  3.   

    建议用in来判断!
    因为in是js中滴类型操作符之一。in
    Evaluates true if the first operand (a string) is the name of a property 
    of the second operand. The second operand must be an object (for example, a constructor).var robot = {jetpack:true}
    alert("jetpack" in robot);
    // alerts true
    alert("x-ray vision" in robot);
    // alerts false
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title> new document </title>
        <meta name="generator" content="editplus" />
        <meta name="author" content="Gao YiXiang" />
        <meta name="email" content="[email protected]" />
        <meta name="keywords" content="javascript dhtml dom" />
        <meta name="description" content="I love web development." />
    </head>
    <body>
        <script type="text/javascript">
        <!--
    var test1 = {
        name:'test1',
        id:'test1_Id',
        size:5
    }
    var test2 = {
        name:'test2',
        id:'test2_Id'
    }for (var p in test2)
    {
        if (p in test1)
        {
            test1[p] = test2[p];
            alert(test1[p]);
        }
    }
        //-->
        </script>
    </body>
    </html>
      

  4.   

    用一个函数即可。
    function swap(){
    for(var temp in test2){
        if(test2[temp]!="undefined"&&test2[temp]!="null"&&test2[temp]!="")
           test1[temp]=test2[temp];
    }
    }
    调用此函数即可。