This is a mirror of official site: http://jasper-net.blogspot.com/

Javascript in Ten Minutes

| Wednesday, January 26, 2011
Basic Types

var intValue = 1;
var floatValue = 3.0;
var stringValue = "This is a string\n";
var sqString = 'This is also a string';
Javascript is a dynamically typed language. Variables are declared with the keyword var. Common simple types are supported.

Arrays

var emptyList = [];
var homogenousList = [1, 2, 3];
var heterogenousList = ["one", 2, 3.0];
Javascript has built-in collection objects. The Array object is a dynamically typed sequence of Javascript values. They are created with the bracket notation [] or with the new operator on the Array object (e.g. new Array(5)).

Property Maps

var emptyMap = {};
var homogenousMap = {"one": 1, "two": 2, "three": 3};
var heterogenousMap = {"one": 1,
                      "two": "two",
                      "three": 3.0};
Along with Arrays are the Object objects. They act as property maps with strings serving as keys to dynamically typed data.

Access

// Dot notation property access
window.alert("Homogenous map property \"one\" "
            + homogenousMap.one);
// Subscript notation property access
window.alert("Homogenous map property \"two\" "
            + homogenousMap["two"]);

Assignment

homogenousMap["one"] = 10;
homogenousMap.two = 20;
Removal

delete homogenousMap["one"];
delete homogenousMap.two;

Iteration

for (var key in heterogenousMap) {
   window.alert("Heterogenous map property \""
                + key
                + "\" = "
                + heterogenousMap[key]);
}

Functions

var callable = function (message) { // <-- notice assignment
   window.alert("Callable called with message = "
                + message);
}

Read more: Javascript

Posted via email from Jasper-net

0 comments: