document.writeln('Hello, world!');

//object declaration in javascript 1.

var stooge = {

    "first-name": "Jerome",

    "last-name": "Howard"

};

 

//object declaration in javascript 2.

var flight = {

    airline: "Oceanic",

    number: 815,

    departure: {

        IATA: "SYD",

        time: "2004-09-22 14:55",

        city: "Sydney"

    },

    arrival: {

        IATA: "LAX",

        time: "2004-09-23 10:42",

        city: "Los Angeles"

    }

};

 

document.writeln(stooge["first-name"]);

document.writeln(flight.airline);

 

stooge['first-name'] = 'Jerome22';

 

document.writeln(stooge["first-name"]);

 

var x = stooge;

x.nickname = 'Curly';

document.writeln(stooge["nickname"]);

stooge.profession = 'actor';

 

document.writeln('number' + '  ' + typeof flight.number);

 

var another_stooge = Object.create(stooge);

another_stooge['first-name'] = 'Harry';

another_stooge['middle-name'] = 'Moses';

another_stooge.nickname = 'Moe';

stooge.profession = 'actor';

delete another_stooge.nickname;

var name;

for (name in another_stooge) {

    if (typeof another_stooge[name] !== 'function') {

        document.writeln(name + ': ' + another_stooge[name]);

    }

}

//Global Abatement

//JavaScript makes it easy to define global variables that can hold all of the assets of

//your application. Unfortunately, global variables weaken the resiliency of programs

//and should be avoided.

//One way to minimize the use of global variables is to create a single global variable

//for your application:

var MYAPP = {};

 

 

//That variable then becomes the container for your application:

 

MYAPP.stooge = {

"first-name": "Joe",

"last-name": "Howard"

};

MYAPP.flight = {

    airline: "Oceanic",

    number: 815,

    departure: {

    IATA: "SYD",

    time: "2004-09-22 14:55",

    city: "Sydney"

},

    arrival: {

    IATA: "LAX",

    time: "2004-09-23 10:42",

    city: "Los Angeles"

}

};

 

//Function in Javascript.

//A function encloses a set of statements. Functions are the fundamental modular unit

//of JavaScript. They are used for code reuse, information hiding, and composition.

//Functions are used to specify the behavior of objects. Generally, the craft of programming

//is the factoring of a set of requirements into a set of functions and data

//structures.

 

var add = function (a, b) {

    return a + b;

};

 

//Function Objects

//Functions in JavaScript are objects. Objects are collections of name/value pairs having

//a hidden link to a prototype object. Objects produced from object literals are

//linked to Object.prototype. Function objects are linked to Function.prototype

//(which is itself linked to Object.prototype). Every function is also created with two

//additional hidden properties: the function’s context and the code that implements

//the function’s behavior.

 

 

 

//The Method Invocation Pattern

//When a function is stored as a property of an object, we call it a method. When a

//method is invoked, this is bound to that object. If an invocation expression contains

//a refinement (that is, a . dot expression or [subscript] expression), it is     invoked as a method:

    // Create myObject. It has a value and an increment

    // method. The increment method takes an optional

    // parameter. If the argument is not a number, then 1

    // is used as the default.

        var myObject = {

            value: 0,

            increment: function (inc) {

                this.value += typeof inc === 'number' ? inc : 1;

            }

        };

    myObject.increment( );

    document.writeln(myObject.value); // 1

    myObject.increment(1);

    document.writeln(myObject.value); // 1

    myObject.increment(2);

    document.writeln(myObject.value); // 3

 

 

    (function () {

        var a = b = 3;

    })();

 

    document.writeln("a defined? " + (typeof a !== 'undefined'));

    document.writeln("b defined? " + (typeof b !== 'undefined'));

 

 

    function foo1() {

        return { bar: "hello"};

    }

 

    function foo2() {

        return

        {

            bar11: "ahello"

        };

    }

 

    document.writeln("foo1 returns:");

    document.writeln(foo1());

    document.writeln("foo2 returns:");

    document.writeln(foo2());

 

function sumDigits(num) {

     var i, sum = 0;                  // can declare two variables at once

 

     for (i = 1; i <= num; i++) {

             sum += i;              // add each number to sum (ie, 1 + 2 + ...+ num)

     }

 

     // Display result

     alert("The sum of the digits from 1 to "+ num + " is:\n\n\t " + sum);

}