Brian's Blog

items I see across my tribes

Optional parameters in Javascript functions

January 04
by briancarter 4. January 2012 08:49

Most programming languages have a mechanism which allows optional parameters to be passed to a function/method and if they are not included in the call, default values are set.

This is not the case in Javascript. But thankfully, it is still possible to set up a function that take optional parameters.  Consider a function defined like so:

 
 
function myFuncName(param1, param2) {

In Javascript, it is perfectly legal to make a call to this function in any of three ways:
myFuncName(1, 2);
myFuncName(1);
myFuncName();

They will all work. In javascript, if you want the parameter to be optional, you need to add code in the function itself to deal with the cases when a parameter value is not given in the call. For example, consider the function above where we want to make the second parameter optional and if the second parameter is not given, we set a default value of '3.14'. That can be done like so:
function myFuncName(param1, param2) {
   // Set the optional parameter if needed
   if ( param2 === undefined ) {
      param2 = '3.14';
   }

   ...
}


You can do this for as many parameters as you like, but you need to be sure to order the parameters so that the "most optional" ones are further to the right since as soon as you leave off one parameter, it starts removing values from the right-hand side of the parameter list.

Categories: Development

SQL Saturday 10/29

October 30
by briancarter 30. October 2011 10:47

image

For a day, hundreds of people gathered to become students – to explore over 5 tracks and dozens of sessions.  Vendors made this event free to all, the University of Louisville provided the place of learning.  A special thanks to the SQL Saturday volunteers and our leader Malathi Mahadevan for making this event a success.

Categories: Development

A comment on Comments

September 25
by briancarter 25. September 2011 10:28

Comments are used to explain your code and may help someone when they are editing the code at a later date.  Problem, comments are different in each language.  I guess if you stuck with /*…*/ you would hit the majority.

CSS:        /*This is a comment*/

HTML:       <!--This is a comment-->

JavaScript: // This is a comment

JavaScript: /* This is a comment */

C#:         // This is a comment

C#:         /* This is a comment */

TSQL:       -- This is a comment

TSQL:       /* This is a comment */

Categories: Development

Learning REST: A Tutorial (1)

September 20
by briancarter 20. September 2011 07:45

We are working on Pondwater 3.0.  One of the exciting updates is the support for REST.  To share some of our learning's, I’ve decided to do a multi-part blog on REST.

1.  What is REST?

REST stands for Representational State Transfer.  It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

  • In many ways, the World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture.

RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.). Later, we will see how much more simple REST is.

  • Despite being simple, REST is fully-featured; there's basically nothing you can do in Web Services that can't be done with a RESTful architecture.

REST is not a "standard". There will never be a W3C recommendation for REST, for example. And while there are REST programming frameworks, working with REST is so simple that you can often "roll your own" with standard library features in languages like C#, Java, RoR, ….

Categories: Development

How to keep HTML footers at the bottom

August 20
by briancarter 20. August 2011 13:28

A diagram describing the footer problem and the ideal solution

Keeping the footer at the bottom of your HTML page can be a pain.  I’ve seen and tried many different techniques.  A very easy way to do this is outlined at the following blog:  http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

Categories: Development

Google Chrome and local development

July 31
by briancarter 31. July 2011 08:05

If you are using Chrome and need to develop locally, there are a few settings that will help.  Below is the command line I use to run Chrome.  Notice the local file access and cookies.

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --enable-file-cookies

Categories: Development

WP7 App Bar Icons not showing

July 18
by briancarter 18. July 2011 07:32

image

I’m working on a pedometer application using the WP7 accelerometer (stay tuned for code and app market).  I added some new images to use on the application bar.  Just an “X” showing – image not available.

Solution - Make sure that the image properties build to action is Content.

Default is Resource.  Now the image shows like I would expect.  Its been awhile since I added new images to the AppBar Smile

image

Categories: Development

Ants have “pedometer-like” brain cells

July 16
by briancarter 16. July 2011 11:05

image

Many of you who studied computer science probably wrote a program based on the Ant colony optimization algorithm.  The algorithm is based on how ants find optimal paths between their colony and a source of food.  The key to this is how ants use their pheromone trails to find the shortest trail to the food source.

Harald Wolf from the University of Ulm conducted experiments on desert ants that showed they have a pedometer in the head.  The experiment showed that by increasing or decreasing the size of ants legs 1/2 way through their journey – they returned a different distance.  Further if extended, less distance if legs shortened.

I’ve been doing a lot of work on pedometers lately and wonder if we too have such built in pedometers.  Watch the video… good stuff to think about.

Ants That Count!

http://www.npr.org/blogs/krulwich/2011/06/01/120587095/ants-that-count

Categories: Development

jQuery = JavaScript Library

July 10
by briancarter 10. July 2011 09:23

Had a friend, good with JavaScript, ask a jQuery question.  Many are hitting the jQuery books hard with the latest direction of web and Windows 8 development.

Question surrounded the $(‘something’)

Answer: if you see $(‘something’) in JavaScript you should read that as being document.getElementById(‘something’). 

jQuery makes the $(‘something’) work in it’s framework.  Nothing magical, jQuery standardizes this so it works across browsers.

Categories: Development

JavaScript: Variables

June 25
by briancarter 25. June 2011 10:40

JavaScript is not a strongly typed language which means you rarely have to concern yourself with the type of data a variable is storing, only what the variable is storing and in JavaScript, variables can store anything, even functions.

Below are some samples.  Notice how isObject is an object so you can do things like - isObject.array[2]; isObject.color; isObject.dog;

<!DOCTYPE HTML>
<html>
<head>
  <title>Learning Javascript</title>
</head>
<body>
<p>JavaScript Variables 

<script>
	var thisIsAString = 'This is a string';
	var alsoAString = '25';
	var isANumber = 25;
	var isEqual = (alsoAString==isANumber); // This is true, they are both 25.
	var isEqual = (alsoAString===isANumber); // False one is a number, the other a string.
	var concat=alsoAString + isANumber; // concat is now 2525
	var addition=isANumber + isANumber; // addition is now 50
	var alsoANumber=3.05; // is equal to 3.05 (usually).
	var floatError=0.06+0.01; // is equal to 0.06999999999999999
	var anExponent=1.23e+3; // is equal to 1230
	var hexadecimal = 0xff; // is equal to 255.
	var octal = 0377; // is equal to 255.
	var isTrue = true; // This is a boolean, it can be true or false.
	var isFalse= false; // This is a boolean, it can be true or false
	var isArray = [0, 'one', 2, 3, '4', 5]; // This is an array.
	var four = isArray[4]; // assign a single array element to a variable.
	// in this case four = '4'
	var isObject = { 'color': 'blue', // This is a Javascript object
	'dog': 'bark',
	'array': [0,1,2,3,4,5],
	'myfunc': function () { alert('This is from isObject.myfunc'); }
	}
	var dog = isObject.dog; // dog now stores the string 'bark';
	isObject.myfunc(); // creates an alert box with the value "This is from isObject.myfunc"
	var someFunction = function() {
		return "I am a function!";
	}
	var alsoAFunction = someFunction; //No () so alsoAFunction becomes a function
	var result = alsoAFunction(); // alsoAFunction is executed here because ()
	// executes the function so result stores the
	// return value of the function which is
	// "I am a function!"		
</script>

</body>
</html>

LAMBDA

A final consideration on variables is that functions themselves can be defined like, and act like variables. Once a function has been defined it can be passed to other functions as an argument (A process knows as lambda), or assigned to other variables just like a string, array or any other JavaScript object.

Generally if you use a function without trailing parenthesis (), the function is treated like a variable and can be passed and assigned. Trailing parenthesis INVOKE the function, executing it and passing back the return value (if any).

Categories: Development


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.