jQuery extendネーミングスペースeachなどの説明
3599 ワード
1.extend拡張
①基本拡張
①基本拡張
extend(dest,src1,src2,src3...);
var newSrc=$.extend({},src1,src2,src3...)// "{}" dest
var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})
result={name:"Jerry",age:21,sex:"Boy"
② 省略dest参数
$.extend({ hello:function(){alert('hello');} });// jQuery
$.fn.extend({ hello:function(){alert('hello');} });// jQuery
$(document).ready(function (){ $.extend({jqueryAlert:function(){alert("jQuery method!");}}); $.fn.extend({ alertWhileClick:function(){ alert("jQuery method!"); } }); $.jqueryAlert(); }); //
③基本的な拡張と深い拡張深く拡張すると、locationのプロパティは2つになりますが、基本的な拡張は確実に上書きされます.// ; var newSrc1 = jQuery.extend( { name: "jonh", location: { city: "Boston" } }, { last: "Resig", location: { state: "MA" } } ); alert(newSrc1.location.city); // ; var newSrc2 = jQuery.extend( true, { name: "John", location: { city: "Boston" } }, { last: "Resig", location: { state: "MA" } } ); alert(newSrc2.location.city);
2.ネーミングスペース
①netにjsonデータがある②netにjsonデータがありません$.extend({net:[{name:"ggf"},{name:"jack"}]}); $.extend($.net,{ hello:function(){alert('hello');} }); $(document).ready(function(){$.net.hello();alert($.net[1].name);});
$.extend({net:{}}); $.extend($.net,{ hello:function(){alert('hello');} }); $(document).ready(function(){$.net.hello();alert($.net[1].name);});
3.jQuery each関数$.each([1,2,3],function(i,val){ //var ; //alert(i); //alert(val); }); var obj = { one:"o", two:"t", three:"r", four:"f"}; $.each(obj, function(key, val) { //alert(val.one);error //alert(val.key);error //alert(obj.key);error //alert(obj.one);o o o o //alert(key); one two three four //alert(val);o t r f }); var obj2 = [{name:"Jack",age:22},{name:"Marry",age:21}]; $.each(obj2, function(key, val) { //alert(key);1 2 //alert(val.name);Jack Marry //alert(obj2[1].age); }); }