I got this example from the Jquery Docs , for the extend function :
- var defaults = { validate: false, limit: 5, name: "foo" };
- var options = { validate: true, name: "bar" };
- // Merge defaults and options, without modifying defaults
- var settings = $.extend( {}, defaults, options );
- var printObj = typeof JSON !== "undefined" ? JSON.stringify : function( obj ) {
-
- var arr = [];
- $.each( obj, function( key, val ) {
- var next = key + ": ";
- next += $.isPlainObject( val ) ? printObj( val ) : val;
- arr.push( next );
- });
- return "{ " + arr.join( ", " ) + " }";
- };
- $( "#log" ).append( "defaults -- " + printObj( defaults ) + "" );
- $( "#log" ).append( "options -- " + printObj( options ) + "" );
- $( "#log" ).append( "settings -- " + printObj( settings ) + "" );
on line 6 is the function ever being called ? i mean does the ternary operator ever evaluate to false ??
i did a console.log('function called'); inside the function and did't get any console.logs
also please have a look at line 6 again :
- var printObj = typeof JSON !== "undefined" ? JSON.stringify : function( obj ) {
if u were a english teacher ,teaching Jquery how would the above statement really read out ?
"check printObj" if it is a a JSON type if it is than JSON.stringify it , else execute the function .
now a few more supplemetary but important questions :
is't JSON.stringify suppose to be taking an argument ??
now i call printObj like this :
- $( "#log" ).append( "defaults -- " + printObj( defaults ) + "" );
going back to line 6 , specifically the below line
- typeof JSON !== "undefined"
what am i checking here ??
if printObj itself is typeof JSON ??
or defaults is typeof JSON ??
the confusion continues as i read line 6 ahead :
now JSON.stringify what ??
if printObj itself is typeof JSON ??
or defaults is typeof JSON ??