Jumat, 29 November 2024

JAVA SCRIPT

function factorial(aNumber)
{
   // If the number is not an integer, round it down.
   aNumber = Math.floor(aNumber);

   // If the number is less than 0, reject it.
   // If the number is 0, its factorial is 1.
   // Otherwise, call this recursive procedure again.
   if (aNumber < 0)
      {
       return -1;
      }
   else if (aNumber == 0)
      {
      return 1;
      }
   else
      {
      return (aNumber * factorial(aNumber - 1));
      }
}

Variable Scope (JavaScript)

JavaScript has two scopes: global and local. If you declare a variable outside of any function definition, it is a global variable, and its value is accessible and modifiable throughout your program. If you declare a variable inside of a function definition, that variable is local. It is created and destroyed every time the function is executed; it cannot be accessed by anything outside the function.

Languages such as C++ also have "block scope." Here, any set of braces "{}" defines a new scope. JavaScript does not support block scopes.

A local variable can have the same name as a global variable, but it is entirely distinct and separate. Consequently, changing the value of one variable has no effect on the other. Inside the function in which the local variable is declared, only the local version has meaning.

// Global definition of aCentaur.
var aCentaur = "a horse with rider,";

// A local aCentaur variable is declared in this function.
function antiquities() 
{
   var aCentaur = "A centaur is probably a mounted Scythian warrior";
}

antiquities();
aCentaur += " as seen from a distance by a naive innocent.";

document.write(aCentaur);

// Output: "a horse with rider, as seen from a distance by a naive innocent."

It is important to note that variables act as if they were declared at the beginning of whatever scope they exist in. Sometimes this results in unexpected behaviors.

var aNumber = 100;
tweak();

function tweak()
{
    // This prints "undefined", because aNumber is also defined locally below.
    document.write(aNumber);

    if (false)
    {
        var aNumber = 123;  
    }
}

When JavaScript executes a function, it first looks for all variable declarations,

var someVariable;

and creates the variables with an initial value of undefined. If a variable is declared with a value,

var someVariable = "something";

then it still initially has the value undefined, and will take on the declared value only when the line containing the declaration is executed, if ever.

JavaScript processes variable declarations before executing any code, so it does not matter whether the declaration is inside a conditional block or some other construct. Once JavaScript has found all the variables, it executes the code in the function. If a variable is implicitly declared inside a function - that is, if it appears on the left-hand-side of an assignment expression but has not been declared with var - then it is created as a global variable.

// This clobbers (over-writes) its parameter, so the change
// is not reflected in the calling code.
function Clobber(param) 
{
    // clobber the parameter; this will not be seen in 
    // the calling code
    param = new Object();
    param.message = "This will not work";
}

// This modifies a property of the parameter, which
// can be seen in the calling code.
function Update(param)
{
    // Modify the property of the object; this will be seen
    // in the calling code.
    param.message = "I was changed";
}

// Create an object, and give it a property.
var obj = new Object();
obj.message = "This is the original";

// Call Clobber, and print obj.message. Note that it hasn't changed.
Clobber(obj);
window.alert(obj.message); // Still displays "This is the original".

// Call Update, and print obj.message. Note that is has changed.
Update(obj);
window.alert(obj.message); // Displays "I was changed".

Using Arrays (JavaScript)

Arrays in JavaScript are sparse. That is, if you have an array with three elements that are numbered 0, 1, and 2, you can create element 50 without worrying about elements 3 through 49. If the array has an automatic length variable (see Intrinsic Objects for an explanation of automatic monitoring of array length), the length variable is set to 51, rather than to 4. You can certainly create arrays in which there are no gaps in the numbering of elements, but you are not required to.

In JavaScript, objects and arrays are almost identical to each other. The two main differences are that normal objects do not have an automatic length property, and arrays do not have the properties and methods of an object.

You address arrays by using brackets, "[]", as shown in the following example. The brackets enclose either a numeric value, or an expression that evaluates to a whole number.

var entryNum = 5;

sample = new Array();

sample[1] = "Maple Street";
sample[entryNum] = 25;

document.write (sample[1]);
document.write (" ");
document.write (sample[entryNum]);

Normally, you use the dot operator "." to access an object's properties. For example,

myObject.aProperty

Here, the property name is an identifier. You can also access an object's properties using the index operator "[]". Here, you are treating the object as an associative array. An associative array is a data structure that allows you to dynamically associate arbitrary data values with arbitrary strings. For example,

myObject["aProperty"] // Same as above.

Although the use of the index operator is more commonly associated with accessing array elements, when used with objects, the index is always the property name expressed as a string literal.

Notice the important difference between the two ways of accessing object properties.

Operator

The property name is treated as

Meaning the property name

Dot "."

an identifier

cannot be manipulated as data

Index "[]"

a string literal

can be manipulated as data

This difference becomes useful when you do not know what the property names will be until runtime (for example, when you are constructing objects based on user input). To extract all the properties from an associative array, you must use the for in loop.














 

Tidak ada komentar: