More JavaScriptBrowser supportWhat you can’t doDebuggingNumbersStrings and charactersSome string methodsMore string methodsbooleanundefined and nullArraysDetermining typesWrappers and conversionsVariablesHTML names in JavaScriptMore about withFunctionsThe Function() constructorFunction literalsFunction namesThe call objectargumentsExample uses of argumentsProperties of functions IProperties of functions IIGlobal and local variablesFunctions and methodsMethods IMethods IIMethods IIIThe EndJan 14, 2019More JavaScript2Browser supportJavaScript works on almost all browsersInternet Explorer uses JScript (referred to in menus as “Active Scripting”), which is Microsoft’s dialect of JavaScriptOlder browsers don’t support some of the newer features of JavaScriptWe will assume modern browser supportEnabling and disabling JavaScript:See http://www.valleyvet.com/si_javascript_help.html3What you can’t doTo protect the visitor to your web pages, you can’t:Read or write user filesHowever, JScript on IE allows ASP scripting, which is how the very destructive JS.Gigger.A@mm worm spreadsTo turn off active scripting in Outlook Express, seehttp://support.microsoft.com/support/kb/articles/Q192/8/46.ASPExecute any other programsConnect to any other computer, except to download another HTML page or to send e-mailDetermine what other sites the user has visitedOpen a very small (less than 100px by 100px) window or an offscreen window (except in IE)4DebuggingMozilla/Netscape has much better debugging tools than IEMozillaSelect Tools => Web Development => JavaScript consoleNetscape 6:Select Tasks => Tools => JavaScript consoleNetscape 4:Select Communicator => Tools => JavaScript consoleAny Mozilla or Netscape:Type javascript: in the location bar and press EnterInternet Explorer:Go to the Preferences... dialog and look for something like Web content => Show scripting error alertsAfter debugging, test your program in IEIE is the most popular browser5NumbersIn JavaScript, all numbers are floating pointSpecial predefined numbers:Infinity, Number.POSITIVE_INFINITY -- the result of dividing a positive number by zeroNumber.NEGATIVE_INFINITY -- the result of dividing a negative number by zeroNaN, Number.NaN (Not a Number) -- the result of dividing 0/0NaN is unequal to everything, even itselfThere is a global isNaN() functionNumber.MAX_VALUE -- the largest representable numberNumber.MIN_VALUE -- the smallest (closest to zero) representable number6Strings and charactersIn JavaScript, string is a primitive typeStrings are surrounded by either single quotes or double quotesThere is no “character” typeSpecial characters are:\0 NUL\b backspace\f form feed\n newline\r carriage return\t horizontal tab\v vertical tab\' single quote\" double quote\\ backslash\xDD Unicode hex DD\xDDDD Unicode hex DDDD7Some string methodscharAt(n)Returns the n th character of a stringconcat(string1, ..., stringN)Concatenates the string arguments to the recipient stringindexOf(substring)Returns the position of the first character of substring in the recipient string, or -1 if not foundindexOf(substring, start)Returns the position of the first character of substring in the given string that begins at or after position start, or -1 if not foundlastIndexOf(substring), lastIndexOf(substring, start)Like indexOf, but searching starts from the end of the recipient string8More string methodsmatch(regexp )Returns an array containing the results, or null if no match is foundOn a successful match:If g (global) is set, the array contains the matched substringsIf g is not set:Array location 0 contains the matched textLocations 1... contain text matched by parenthesized groupsThe array index property gives the first matched positionreplace(regexp, repl ac e ment)Returns a new string that has the matched substring replaced with the replaceme ntsearch(regexp)Returns the position of the first matched substring in the given string, or -1 if not found.9booleanThe boolean values are true and falseWhen converted to a boolean, the following values are also false:0"0" and '0'The empty string, '' or ""undefinednullNaN10undefined and nullThere are special values undefined and nullundefined is the only value of its “type”This is the value of a variable that has been declared but not defined, or an object property that does not existvoid is an operator that, applied to any value, returns the value undefinednull is an “object” with no propertiesnull and undefined are == but not ===11ArraysAs in C and Java, there are no “true” multidimensional arraysHowever, an array can contain arraysThe syntax for array reference is as in C and JavaExample: var a = [ ["red", 255], ["green", 128] ]; var b = a[1][0]; // b is now "green" var c = a[1]; // c is now ["green", 128] var d = c[1]; // d is now 12812Determining typesThe unary operator typeof returns one of the following strings: "number", "string", "boolean", "object", "undefined", and "function"typeof null is "object"If myArray is an array, typeof myArray is "object"To distinguish between different types of objects,myObje ct instanceof ConstructorThe Constructor should be an object that is a constructor functionIt is an error if the right-hand side is not an object at allmyObje ct.constructor == ConstructormyObje ct.toString() == "ConstructorName"13Wrappers and conversionsJavaScript has “wrapper” objects for when a primitive value must be treated as an objectvar s = new String("Hello"); // s is now a Stringvar n = new Number(5); // n is now a Numbervar b = new Boolean(true); // b is now a BooleanBecause JavaScript does automatic conversions as needed, wrapper objects are hardly ever neededJavaScript has no “casts,” but conversions can be forcedvar s = x + ""; // s is now a stringvar n = x - 0; // n is now a numbervar b = !!x; // b is now a booleanBecause JavaScript does automatic conversions as needed, explicit conversions are hardly ever needed14VariablesEvery variable is a property of an objectWhen JavaScript starts, it creates a global objectIn client-side JavaScript, the window is the global objectIt can be referred to as window or as thisThe
View Full Document