Jq $全局對象擴展

1.$.extend(src)
解釋:參數爲一個參數時,在jq全局對象中擴展屬性或者函數。

$.extend({

		hello:function(){
			alert("Hello World!");
		},
		ok:function(){
			alert("我已經在jq全局對象中啦!!!");
		}
	})
			
		$.hello();
		$.ok();
重載原型
2.$.extend(boolean,dest,src1,src2,src3,....)

解釋:boolean 參數布爾型,意思是是否深度拷貝,
     dest,src1,src2,src3 參數,意思是把後面的src1,src2,src3合併到dest參數中
     此時boolean發揮它的做用

如:
   $(function() {
			var result=$.extend(true,
			       {},
			       {name:"zhangsan",age:26,location:{address:"nc",phone:18679125671,status:2}},
		           {lastName:"lisi",job:"programe",location:{address:"wh",phone:12345678912}}
		        )
		console.log(result);
		})

咱們能夠看到結果是javascript

{name:"zhangsan",age:26,location:{address:"nc",phone:18679125671,status:2}},
{lastName:"lisi",job:"programe",location:{address:"wh",phone:12345678912}}java

它們合併爲一個對象,而且若是有重複的屬性,後一個對象和覆蓋前一個對象的屬性值。由於boolean咱們設置爲true,那麼對象裏的子對象和進行了合併。下面咱們看boolean爲false.函數

$(function() {
			var result=$.extend(false,
			       {},
			       {name:"zhangsan",age:26,location:{address:"nc",phone:18679125671,status:2}},
		           {lastName:"lisi",job:"programe",location:{address:"wh",phone:12345678912}}
		        )
		console.log(result);
		})

從結果上咱們能夠看出,boolean設置爲false,location屬性的值沒有進行合併。直接由後面的覆蓋了面的code

相關文章
相關標籤/搜索