null
Unlike undefined, null is often set to indicate that something has been declared but has been defined to be empty. In a Boolean context, the value of null is considered a false value in JavaScript.
Note: Null is a true primitive-type within the JavaScript language, of which null (note case) is the single value. As such, when performing checks that enforced type checking, the null value will not equal other false types. Surprisingly, null is considered an object by typeof.
alert(null == undefined); // unenforced type during check, displays true
alert(null === undefined); // enforce type during check, displays false
alert(typeof null === 'object'); // true
alert(null == undefined); // unenforced type during check, displays true
alert(null === undefined); // enforce type during check, displays false
alert(typeof null === 'object'); // true
Number
Numbers are represented in binary as IEEE-754 Doubles, which provides an accuracy nearly 16 significant digits. Because they are floating point numbers, they do not always exactly represent real numbers, including fractions.
This becomes an issue when comparing or formatting numbers. For example:
alert(0.2 + 0.1 == 0.3); // displays false alert(0.94 - 0.01); // displays 0.9299999999999999
NaN; // The Not-A-Number value, also returned as a failure in // string-to-number conversions
Number.NaN - Use the isNaN() global function to check if a value is a NaN value.
isNaN("123") // false
isNaN("Hello") // true
The Number constructor, or a unary + or -, may be used to perform explicit numeric conversion:
var myString = "123.456";
var myNumber1 = Number(myString);
var myNumber2 = +myString;
[edit]String
the preferred way when accessing individual characters within a string because it also works in non-modern browsers:
var h = greeting.charAt(0);
In modern browsers, individual characters within a string can be accessed (as strings with only a single character) through the same notation as arrays:
var h = greeting[0];
However, JavaScript strings are immutable:
greeting[0] = "H"; // Not working.
Applying the equality operator ("==") to two strings returns true if the strings have the same contents, which means: of same length and same cases (for alphabets). Thus:
var x = "world";
var compare1 = ("Hello, " +x == "Hello, world"); // Now compare1 contains true
You cannot use quotes of the same type inside the quotes unless they are escaped.
var x = '"Hello, world!" he said.' // Just fine.
var x = ""Hello, world!" he said." // Not good.
var x = "\"Hello, world!\" he said." // That works by replacing " with \"
Array
An Array is a JavaScript object prototyped from the Array constructor specifically designed to store data values indexed by integer keys. Arrays, unlike the basic Object type, are prototyped with methods and properties to aid the programmer in routine tasks (for example, join, slice, and push).
Arrays are implemented so that only the elements defined use memory; they are "sparse arrays". Setting myArray[10] = 'someThing' and myArray[57] = 'somethingOther' only uses space for these two elements, just like any other object. Thelength of the array will still be reported as 58.
One can use the object declaration literal to create objects that behave much like associative arrays in other languages:
dog = {color: "brown", size: "large"};
dog["color"]; // results in "brown"
dog.color; // also results in "brown"
One can use the object and array declaration literals
cats = [{color: "brown", size: "large"},
{color: "black", size: "small"}];
cats[0]["size"]; // results in "large"
dogs = {rover: {color: "brown", size: "large"},
spot: {color: "black", size: "small"}};
dogs["spot"]["size"]; // results in "small"
dogs.rover.color; // results in "brown"
[edit]
For ... in loop
The syntax of the JavaScript For ... in loop is as follows:
for (var property_name in some_object) {
//statements using some_object[property_name];
}
- Iterates through all enumerable properties of an object.
- Iterates through all used indices of array including all user-defined properties of array object if any. Thus it may be better to use a traditional for loop with a numeric index when iterating over arrays.
- There are differences between the various web browsers with regard to which properties will be reflected with the for...in loop statement. In theory, this is controlled by an internal state property defined by the ECMAscript standard called "DontEnum", but in practice each browser returns a slightly different set of properties during introspection. It is useful to test for a given property using
if (some_object.hasOwnProperty(property_name)) { ... }. Thus, adding a method to the array prototype withArray.prototype.newMethod = function() {...} may cause for ... in loops to loop over the method's name.
With
The with statement sets the default object for the set of statements that follow.
with (document) {
var a = getElementById('a');
var b = getElementById('b');
var c = getElementById('c');
};
Functions
If you exit the function without a return statement, the value undefined is returned.
function gcd(segmentA, segmentB) {
var diff = segmentA - segmentB;
if (diff == 0)
return segmentA;
return diff > 0 ? gcd(segmentB, diff) : gcd(segmentA, -diff);
}
alert(gcd(60, 40)); // 20
var mygcd=gcd; // mygcd is a reference to the same function as gcd. Note no argument ()s.
alert(mygcd(60, 40)); // 20
Functions are first class objects and may be assigned to other variables.
[edit]
No comments:
Post a Comment