Java Script Functions Objects Java Script Functions Function

Java. Script Functions & Objects

Java. Script Functions Function Description decode. URI() Decodes an encoded URI decode. URIComponent() Decodes an encoded URI component encode. URI() Encodes a string as a URI encode. URIComponent() Encodes a string as a URI component escape() Encodes a string eval() Evaluates a string and executes it as if it was script code is. Finite() Checks if a value is a finite number is. Na. N() Checks if a value is not a number parse. Float() Parses a string and returns a floating point number parse. Int() Parses a string and returns an integer unescape() Decodes a string encoded by escape()

escape(), unescape() var u. Url = "http: //www. google. com. tw/search? q=暨南大學"; var e. Url = "%u 570 B%u 9053%u 516 D%u 865 F"; document. write(u. Url + " "); document. write("escape: "+ escape(u. Url) + " "); document. write(e. Url + " "); document. write("unescape: "+ unescape(e. Url) + " "); http: //www. google. com. tw/search? q=暨南大學 escape: http%3 A//www. google. com. tw/search%3 Fq%3 D%u 66 A 8%u 5357%u 5927%u 5 B 78 %u 570 B%u 9053%u 516 D%u 865 F unescape: 國道六號

Java. Script Objects Boolean Number String Array Math Date Reg. Exp

Boolean Object Create Boolean objects with an initial value of false: var my. Boolean=new Boolean(); var my. Boolean=new Boolean(0); var my. Boolean=new Boolean(null); var my. Boolean=new Boolean(""); var my. Boolean=new Boolean(false); var my. Boolean=new Boolean(Na. N); Create Boolean objects with an initial value of true: var my. Boolean=new Boolean(1); var my. Boolean=new Boolean(true); var my. Boolean=new Boolean("true"); var my. Boolean=new Boolean("false"); var my. Boolean=new Boolean("Richard");

Number Object var my. Num=new Number(86); Properties: Þ MAX_VALUE, MIN_VALUE, Na. N, NEGATIVE_INFINITY, POSITIVE_INFINITY Methods: Þ Þ to. Exponential(num) to. Fixed(num) to. Precision(num) to. String( ) num = 5000 num. to. Exponential(2) = 5. 00 e+3 num. Obj = 6000 num. Obj. to. Exponential(1) = 6. 0 e+3 num 2 = 3. 456 num 2. to. Fixed(1) = 3. 5 num 2. to. Precision(3) = 3. 46

String Object var my. Str=new String("Hello World!"); Properties: length Methods: char. At(index), char. Code. At(index), concat(string. X, . . . , string. X) from. Char. Code(num. X, . . . , num. X) index. Of(searchvalue, fromindex), last. Index. Of(searchvalue, fromindex) match(searchvalue), search(searchvalue) replace(findstring, newstring) to. Lower. Case( ), to. Upper. Case( ) slice(start, end), substr(start, length), substring(start, stop) split(separator, howmany)

slice( ) vs. substring( ) str = "Hello happy world!" index 012345678901234567 876543210987654321 str. slice(6, 13) str. substring(6, 13) str. slice(6) str. substring(6) str. slice(13, 6) str. substring(13, 6) str. slice(-16, 8) str. substring(-16, 8) (length-index) "happy w" "happy world!" "" "happy w" "llo ha" "Hello ha"

split(separator, howmany) var str 1 = "Java. Script, CSS, XML, Dynamic HTML"; var arr 1 = str 1. split(", "); arr 1[0] Java. Script arr 1[1] CSS arr 1[2] XML arr 1[3] Dynamic HTML var str 2 = 'Content-type: multipart/mixed; boundary="---xxyy"'; var arr 2 = str 2. split(": ", 2); arr 2[0] Content-type arr 2[1] multipart/mixed; boundary="----xxyy"

Array Object var arr 1 = new Array(); var arr 2 = new Array(4); var arr 3 = new Array(2009, "April", true); Properties: length Methods: splice() shift() unshift() concat(array. X, . . . , array. X) pop(), push(element 1, element 2, . . . , element. X) shift( ), unshift(element 1, element 2, . . . , element. X) slice(start, end), splice(index, howmany, element 1, . . . , element. X) reverse( ), sort(sortbyfunc) join(separator) pop() push()

splice(index, howmany, element 1, . . . , element. X) var arr = [0, 1, 2, 3, 4, 5, 6]; arr. splice(2, 0, "a 1", "a 2", "a 3"); document. write(arr+" "); // 0, 1, a 2, a 3, 2, 3, 4, 5, 6 arr. splice(2, 3); document. write(arr+" "); // 0, 1, 2, 3, 4, 5, 6 arr. splice(3, 1, "a 1", "a 2", "a 3"); document. write(arr+" "); // 0, 1, 2, a 1, a 2, a 3, 4, 5, 6
![sort(sortbyfunc) var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] = sort(sortbyfunc) var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] =](http://slidetodoc.com/presentation_image_h2/43341259e04263a592e7d67e2c1201d9/image-12.jpg)
sort(sortbyfunc) var arr = new Array(6); arr[0] = 10; arr[1] = 5; arr[2] = 40; arr[3] = 25; arr[4] = 1000; arr[5] = 1; document. write(arr + " "); // 10, 5, 40, 25, 1000, 1 document. write(arr. sort()+ " "); // 1, 1000, 25, 40, 5 document. write(arr. sort(sort. By. Num)+ " "); // 1, 5, 10, 25, 40, 1000 function sort. By. Num(a, b) { return a-b; }
![join(separator) var arr 1 = ["Java. Script", "CSS", "XML", "Dynamic HTML"]; var str 1 join(separator) var arr 1 = ["Java. Script", "CSS", "XML", "Dynamic HTML"]; var str 1](http://slidetodoc.com/presentation_image_h2/43341259e04263a592e7d67e2c1201d9/image-13.jpg)
join(separator) var arr 1 = ["Java. Script", "CSS", "XML", "Dynamic HTML"]; var str 1 = arr 1. join(); var str 2 = arr 1. join(", "); document. write(str 1+" "); document. write(str 2+" "); Java. Script, CSS, XML, Dynamic HTML

Math Object Math is a static object. var area = 2*2* Math. PI; Properties: Property Description E Returns Euler's constant (approx. 2. 718) LN 2 Returns the natural logarithm of 2 (approx. 0. 693) LN 10 Returns the natural logarithm of 10 (approx. 2. 302) LOG 2 E Returns the base-2 logarithm of E (approx. 1. 442) LOG 10 E Returns the base-10 logarithm of E (approx. 0. 434) PI Returns PI (approx. 3. 14159) SQRT 1_2 Returns the square root of 1/2 (approx. 0. 707) SQRT 2 Returns the square root of 2 (approx. 1. 414)

Math's Method Description random() abs(x) max(x, y, …), min(x, y, …) round(x) ceil(x), floor(x) pow(x, y) exp(x) log(x) sqrt(x) cos(x), acos(x) sin(x), asin(x) tan(x), atan 2(y, x) a random number r, 0 ≦ r < 1 , i. e. r in [0, 1) the absolute value of a number the number with the highest, lowest value of x, y, … Rounds a number to the nearest integer rounded upwards, downwards to the nearest integer the value of x to the power of y the value of Ex the natural logarithm (base E) of a number the square root of a number the cosine, arccosine of a number the sine, arcsine of a number the tangent, arctangent of x, the angle theta of an (x, y) point

Math. random() var rand 1 = Math. random(); var rand 2 = my. Random(1, 10); var rand 3 = my. Int. Random(1, 49); // [0, 1) // [1, 10) // integer in [1, 49] // a random number in [a, b) function my. Random(a, b) { return a+Math. random()*(b-a); } // an random integer in [a, b] function my. Int. Random(a, b) { return Math. floor(a+Math. random()*(b-a+1)); }

Date Object Constructors new Date( ) var today = New Date(); // Current date and time new Date ( [ year [, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ) var birth. Day = new Date(1978, 0, 31); //Jan 31 00: 00 1978 new Date (value) var day 1970 = new Date(0); // Jan 1 00: 00 1970 new Date(datestring) var someday = new Date("Mar 29 13: 01 2009");

Date's Methods (1/3) Methods Description get. Full. Year() the year, as a four-digit number get. Month() the month (from 0 -11) get. Date() the day of the month (from 1 -31) get. Day() the day of the week (from 0 -6) get. Hours() the hour (from 0 -23) get. Minutes() the minutes (from 0 -59) get. Seconds() the seconds (from 0 -59) get. Milliseconds() the milliseconds (from 0 -999) get. Time() milliseconds since midnight Jan 1, 1970 get. Timezone. Offset() Difference in minutes between local time and Greenwich Mean Time (GMT) get. UTCFull. Year(), get. UTCMonth(), get. UTCDate(), get. UTCDay(), get. UTCHours(), get. UTCMinutes(), get. UTCSeconds(), get. UTCMilliseconds()

Date's Methods (2/3) Methods Description set. Full. Year() the year, as a four-digit number set. Month() the month (from 0 -11) set. Date() the day of the month (from 1 -31) set. Hours() the hour (from 0 -23) set. Minutes() the minutes (from 0 -59) set. Seconds() the seconds (from 0 -59) set. Milliseconds() the milliseconds (from 0 -999) set. Time() milliseconds since midnight Jan 1, 1970 set. UTCFull. Year(), set. UTCMonth(), set. UTCDate(), set. UTCHours(), set. UTCMinutes(), set. UTCSeconds(), set. UTCMilliseconds()

Date's Methods (3/3) Methods Description to. String() Convert to a string to. Date. String() The date portion in readable form to. Time. String() The time portion in readable form to. Locale. String() Convert to a string, according to local time to. Locale. Date. String() Convert to a string, according to local time, and returns the date portion to. Locale. Time. String() Convert to a string, according to local time, and returns the time portion to. UTCString() Convert to a string, according to universal time parse() UTC() Takes a date string and returns the number of milliseconds since midnight of January 1, 1970 Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time
- Slides: 20