方法中的參數(shù)調(diào)換位置之后objInfo.title為什么是undifined?
方法中的參數(shù)調(diào)換位置之后objInfo.title為什么是undifined?
<!DOCTYPE?html> <html> ????<head> ????????<title>使用$.extend()擴(kuò)展Object對(duì)象</title> ????????<link?href="style.css"?rel="stylesheet"?type="text/css"?/> ????????<script?src="http://idcbgp.cn/data/jquery-1.8.2.min.js"?type="text/javascript"></script> ????</head> ???? ????<body> ????????<div?id="divtest"> ????????????<div?class="title"> ????????????????<span?class="fl">合并原有對(duì)象</span>? ????????????</div> ????????????<div?class="content"> ????????????????<div?class="tip"></div> ????????????</div> ????????</div> ???????? ????????<script?type="text/javascript"> ????????????$(function?()?{ ????????????????var?objInfo?=?{?name:?"1"?}; ????????????????var?objMess?=?{?name:?"白富美,",?title:?"歡迎與我聯(lián)系!"?};? ????????????????var?objNewInfo?=$.extend(objMess,objInfo); ????????????????var?strTmp?=?"<b>對(duì)象?白富美?合并后</b>:<br/><br/>"; ????????????????strTmp?+=?objNewInfo.name?+?objInfo.title; ????????????????//顯示在頁(yè)面中 ????????????????$(".tip").show().append(strTmp); ????????????}); ????????</script> ????</body> </html>
2018-11-20
你把方法里的變量名寫反了,正確順序$.extend(objInfo,objMess);。你的順序$.extend(objMess,objInfo);,把變量名反回來(lái)就行了。
2018-06-20
在擴(kuò)展對(duì)象時(shí),兩個(gè)對(duì)象將進(jìn)行合并,當(dāng)存在相同屬性名時(shí),后者將覆蓋前者,而objInfo中沒(méi)有title的屬性
var?objNewInfo?=$.extend(objMess,objInfo);????//是后者objInfo把前者相同屬性name覆蓋了
strTmp?+=?objNewInfo.name?+?objInfo.title;????????//objInfo中無(wú)該屬性,所以為undefined(在js中所有未定義的屬性輸出顯示時(shí)是undefined)
2017-07-07
這個(gè)合并不算是合并,相當(dāng)于覆蓋,后面的obj對(duì)象值為空,覆蓋了前面的obj對(duì)象,當(dāng)然undefined
2017-05-17
不知道是什么機(jī)制,坐等大神解答
2017-05-06
不會(huì)啊。我換了之后是(歡迎與我聯(lián)系!白富美,)