Ask Experts Questions for FREE Help !
Ask
    mhanson's Avatar
    mhanson Posts: 1, Reputation: 1
    New Member
     
    #1

    Apr 27, 2011, 12:59 PM
    Anonymous Functions and Arguments
    Currently I'm reading Test-Driven JavaScript Development by Christain Johansen. One of the examples uses a JavaScript version of strftime():

    strftime.js
    Code:
    Date.prototype.strftime = (function ()
    {
    	function strftime(format)
    	{
    		var date = this;
    		return (format + "").replace(/%([a-zA-Z])/g,
    		function (m, f)
    		{
    			var formatter = Date.formats && Date.formats[f];
    			if (typeof formatter == 'function')
    			{
    				return formatter.call(Date.formats, date);
    			}
    			else if (typeof formatter == 'string')
    			{
    				return date.strftime(formatter);
    			}
    			return f;
    		});
    	}
    
    	function zeroPad(num)
    	{
    		return (+num < 10 ? '0' : '') + num;
    	}
    
    	Date.formats =
    	{
    		d: function (date)
    		{
    			return zeroPad(date.getDate());
    		},
    
    		m: function (date)
    		{
    			return zeroPad(date.getMonth() + 1);
    		},
    
    		y: function (date)
    		{
    			return zeroPad(date.getYear() % 100);
    		},
    
    		Y: function (date)
    		{
    			return date.getFullYear();
    		},
    
    		F: '%Y-%m-%d',
    		D: '%m/%d/%y'
    	};
    	return strftime;
    } ());
    Starting in line 6 the strftime function uses the replace method to return a certain date format. The first part is a regular expression /%([a-zA-Z])/g to identify what needs to be replaced. The second part of the replace method lists the text that will do the replacing. In this example the second part is an anonymous function. When I step through the code using Firefox the arguments (m, f) are passed to the anonymous function. Where are these arguments coming from? How are the determined? Below is the rest of the code.

    strftime.test.01-08.js
    Code:
    function assert(message, expr)
    {
    	if (!expr)
    	{
    		throw new Error(message);
    	}
    	assert.count++;
    	return true;
    }
    assert.count = 0;
    var date = new Date(2009, 9, 2);
    
    try
    {
    	assert('%Y should return full year', date.strftime("%Y") === '2009');
    	assert('%m should return month', date.strftime("%m") === '10');
    	assert('%d should return date', date.strftime("%d") === '02');
    	assert('%y should return year as two digits', date.strftime("%y") === '09');
    	assert('%F should act as %Y-%m-%d', date.strftime("%F") === '2009-10-02');
    	console.log(assert.count + ' tests OK');
    }
    catch(e)
    {
    	console.log('Test failed: ' + e.message);
    }
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<title>Listing 01-04</title>
    		<script src="strftime.js" type="text/javascript"></script>
    		<script src="strftime.test.01-08.js" type="text/javascript"></script>
    	</head>
    	<body>
    
    	</body>
    </html>

Check out some similar questions!

How to solve trig functions applications for advanced functions? [ 2 Answers ]

A point on the ocean rises and falls as wave pass Suppose that a wave passes every 4 s, and the height of each wave from the crest to the trough in 0.5 m. a) how do we graph the height of the point relative to tis average height for a complete cycle, starting at the crest of a wave. And how do...

Composition Functions and Inverse Functions [ 3 Answers ]

Wow! These things are boogers! :eek: any way to make these things easier?

Anonymous callers [ 1 Answers ]

How can I see who is calling my tmobile phone private?

Anonymous Breakouts [ 2 Answers ]

My boyfriend has been suffering with these horrific breakouts for the past 2 1/2 yrs. And he's been unable to seek medical attention. We know that its not hives, he gets these whelp like bumps on his body that mainly come at night, it usually starts by itching and is the same color as his skin and...

My miss anonymous [ 2 Answers ]

I know I have posted already posted a thread on how to talk to girls, but there is one thing There is this girl at school, she is really pretty, and I have noticed her looking at me at times, and I know she has noticed me looking at her. I have had so many chances to talk to her but I didn't...


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.