Writing JavaScript

JavaScript is not unlike C#. In fact, they both have their roots in Java. Hence, you will find many similarities—if you can read and write C#, you can read and write JavaScript. They both use brackets to group sections of code; end lines with semicolons; concatenate strings with +; use the logic operators && (and), || (or), ! (not); call properties and methods with dot notation; and use this as a keyword. The similarities continue from there: global and local scoping rules, switch statements, looping constructs (for...next, do ...while), conditions (If...Else), objects with properties and methods, functions with parameters, events, comments, operators, variables, functions, and more.

JavaScript does simplify data types using only numeric, string, and Boolean for simple types. Of course, you can create complex objects that include these types (and other objects). JavaScript also simplifies collections using only an array. However, all these items pretty much work the same as they do in C#. In fact, take a look at the following two code segments: one C# and one JavaScript. Notice that they are essentially the same; the primary exception is that the C# code is written inside a class (in this case, a Console application with the Main() method). The JavaScript code is just script (a class definition is not required). The other difference is that C# requires a delegate for a lambda expression (findBike). JavaScript simplifies this by allowing you to assign the function to a variable. In general, however, you should be able to read and write using both languages with a few semantic differences.

C#

delegate int search(string text);

static void Main(string[] args)
{
  string[] bikes = new string[]
    { "BMX", "10-Speed", "Cruiser", "Road", "Mountain" };
  var searchBike = "Road";

  //Lambda expression using a delegate
  search findBike = (string text) => {
    for (int i = 0; i < bikes.Length; i++)
    {
      if (text == bikes[i])
      {
        return i;
      }
    }
    return -1;
  };

  Console.WriteLine("Bike found at: " + findBike(searchBike).ToString());
  Console.ReadLine();
}

JavaScript

var bikes =
  [ "BMX", "10-Speed", "Cruiser", "Road", "Mountain" ];
var searchBike = "Road";

//Assign a function to a variable.
var findBike = function(text) {
  for (i = 0; i < bikes.length; i++)
  {
    if (text == bikes[i])
    {
      return i;
    }
  }
  return -1;
};

alert("Bike found at: " + findBike(searchBike));

The power of JavaScript is not necessarily the language; it is what you are able to do with the language. These tasks include manipulating the browser window and the document object model (DOM) on the user’s computer running your page. This is the true power of JavaScript.

There are many ways to write functions, create objects, and manipulate the DOM; this variety is what makes JavaScript sometimes difficult to read and understand. The core language, however, should be pretty familiar. First, let’s start with a deeper overview of some of the unique elements of JavaScript that make it different to C# developers. We will then look at harnessing some of the power to work with web pages inside a client’s browser. Once you have adapted to working with JavaScript, you should become effective very quickly.


Note

If you really do need to start at the beginning with JavaScript, we hope you use this chapter as a primer. There are many other great resources out there, such as Sams Teach Yourself Java in 24 Hours.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset