$.extend()用法總結(轉載)
阿新 • • 發佈:2018-10-31
1.用法描述
$.extend(setting,option)用於合併物件setting和物件option並將合併後的值返回給setting
常規用法(1)
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
jQuery.extend(settings, options);
執行結果settings == { validate: true, limit: 5, name: “bar” }
常規用法(2)
不改變setting型別
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
//設定一個空類
var x=jQuery.extend({},settings, options);
結果 settings = { validate: false, limit: 5, name: “foo” };
x== { validate: true, limit: 5, name: “bar” }
好處:值被合併到這個空物件上,保護了預設值。