试试for(var p in test1){
if(typeof(test2[p])=="undefined")test2[p]=test1[p];
}
if(typeof(test2[p])=="undefined")test2[p]=test1[p];
}
解决方案 »
- 不明白怎么回事,请指教
- 急~~ Ext 中 load({params : { start : 0, limit : 20 }});
- 求助 Extjs Gird的问题
- 急救 我在网页中嵌入flash 在IE6下可以 FF/ie7 flash接收不到数据 是FLASH的问题还是嵌入代码的问题?
- 这个表单验证电子邮件的js代码在FF和chrome下不正常
- 下面这段代码中document.zcform.username.select();是什么意思
- 有js高手知道>>>是什么意思!与>>有何区别
- 使用拖拽实现拷贝功能时出现的问题
- 如何隐葳和展开<tr>?
- Composition是什么意思??--菜鸟在线等待!!
- js
- javascript open
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>
for (var Member in test2){
if (test1[p])
test1[p]=test2[p];
}
因为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>
function swap(){
for(var temp in test2){
if(test2[temp]!="undefined"&&test2[temp]!="null"&&test2[temp]!="")
test1[temp]=test2[temp];
}
}
调用此函数即可。