JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an...
Transcript of JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an...
JavaScript FunctionsFunction Description
decodeURI() Decodes an encoded URI
decodeURIComponent()
Decodes an encoded URI component
encodeURI() Encodes a string as a URI
encodeURIComponent()
Encodes a string as a URI component
escape() Encodes a string
eval() Evaluates a string and executes it as if it was script code
isFinite() Checks if a value is a finite number
isNaN() Checks if a value is not a number
parseFloat() Parses a string and returns a floating point number
parseInt() Parses a string and returns an integer
unescape() Decodes a string encoded by escape()
escape(), unescape()var uUrl = "http://www.google.com.tw/search?q=暨南大學 ";var eUrl = "%u570B%u9053%u516D%u865F";document.write(uUrl + "<br />");document.write("escape: "+ escape(uUrl) + "<br /> <br />");document.write(eUrl + "<br />");document.write("unescape: "+ unescape(eUrl) + "<br />");
http://www.google.com.tw/search?q=暨南大學escape: http%3A//www.google.com.tw/search%3Fq%3D%u66A8%u5357%u5927%u5B78
%u570B%u9053%u516D%u865Funescape: 國道六號
Boolean Object Create Boolean objects with an initial value of false:
var myBoolean=new Boolean(); var myBoolean=new Boolean(0); var myBoolean=new Boolean(null); var myBoolean=new Boolean(""); var myBoolean=new Boolean(false); var myBoolean=new Boolean(NaN);
Create Boolean objects with an initial value of true:var myBoolean=new Boolean(1); var myBoolean=new Boolean(true);var myBoolean=new Boolean("true");var myBoolean=new Boolean("false"); var myBoolean=new Boolean("Richard");
Number Object
var myNum=new Number(86); Properties:
MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY
Methods: toExponential(num) toFixed(num) toPrecision(num) toString( )
num = 5000 num.toExponential(2) = 5.00e+3numObj = 6000numObj.toExponential(1) = 6.0e+3num2 = 3.456num2.toFixed(1) = 3.5num2.toPrecision(3) = 3.46
String Objectvar myStr=new String("Hello World!"); Properties:
length Methods:
charAt(index), charCodeAt(index), concat(stringX, stringX,..., stringX)fromCharCode(numX, numX,..., numX)indexOf(searchvalue, fromindex), lastIndexOf(searchvalue, fromindex)match(searchvalue), search(searchvalue)replace(findstring, newstring)toLowerCase( ), toUpperCase( ) slice(start,end), substr(start,length), substring(start,stop)split(separator, howmany)
slice( ) vs. substring( )str = "Hello happy world!" 012345678901234567 876543210987654321str.slice(6,13) "happy w"str.substring(6,13) "happy w"str.slice(6) "happy world!"str.substring(6) "happy world!"str.slice(13, 6) "" str.substring(13,6) "happy w"str.slice(-16, 8) "llo ha"str.substring(-16,8) "Hello ha"
(length-index)
index
split(separator, howmany)
var str1 = "JavaScript, CSS, XML, Dynamic HTML";var arr1 = str1.split(", ");
arr1[0] arr1[1] arr1[2] arr1[3]JavaScript CSS XML Dynamic HTML
var str2 = 'Content-type: multipart/mixed; boundary="----xxyy"';
var arr2 = str2.split(": ", 2);
arr2[0] arr2[1]Content-type multipart/mixed; boundary="----xxyy"
Array Objectvar arr1 = new Array();var arr2 = new Array(4);var arr3 = new Array(2009, "April", true); Properties:
length Methods:
concat(arrayX, arrayX, ..., arrayX)pop(), push(element1, element2, ..., elementX)shift( ), unshift(element1, element2, ..., elementX)slice(start,end), splice(index, howmany, element1, ..., elementX)reverse( ), sort(sortbyfunc)join(separator)
shift()
unshift()
pop()
push()
splice()
splice(index, howmany, element1, ..., elementX)
var arr = [0,1,2,3,4,5,6];arr.splice(2, 0, "a1", "a2", "a3");document.write(arr+"<br />");arr.splice(2, 3);document.write(arr+"<br />");arr.splice(3, 1, "a1", "a2", "a3");document.write(arr+"<br />");
// 0,1,a1,a2,a3,2,3,4,5,6
// 0,1,2,3,4,5,6
// 0,1,2,a1,a2,a3,4,5,6
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 + "<br />"); //
10,5,40,25,1000,1document.write(arr.sort()+ "<br />"); // 1,10,1000,25,40,5document.write(arr.sort(sortByNum)+ "<br />"); //
1,5,10,25,40,1000function sortByNum(a, b) { return a-b;}
join(separator)
var arr1 = ["JavaScript", "CSS", "XML", "Dynamic HTML"];
var str1 = arr1.join();var str2 = arr1.join(", ");document.write(str1+"<br />");document.write(str2+"<br />");
JavaScript,CSS,XML,Dynamic HTML
JavaScript, 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) LN2 Returns the natural logarithm of 2 (approx.
0.693) LN10 Returns the natural logarithm of 10 (approx.
2.302) LOG2E Returns the base-2 logarithm of E (approx.
1.442) LOG10E Returns the base-10 logarithm of E (approx.
0.434) PI Returns PI (approx. 3.14159) SQRT1_2 Returns the square root of 1/2 (approx. 0.707) SQRT2 Returns the square root of 2 (approx. 1.414)
Math's MethodsMethod Description
random() a random number r, 0 ≦ r < 1 , i.e. r in [0,1)
abs(x) the absolute value of a number
max(x,y, …), min(x,y, …)
the number with the highest, lowest value of x, y, …
round(x) Rounds a number to the nearest integer
ceil(x), floor(x) rounded upwards, downwards to the nearest integer
pow(x,y) the value of x to the power of y
exp(x) the value of Ex
log(x) the natural logarithm (base E) of a number
sqrt(x) the square root of a number
cos(x), acos(x) the cosine, arccosine of a number
sin(x), asin(x) the sine, arcsine of a number
tan(x), atan(x), atan2(y,x)
the tangent, arctangent of x,the angle theta of an (x,y) point
Math.random()
var rand1 = Math.random(); // [0,1)var rand2 = myRandom(1, 10); // [1, 10)var rand3 = myIntRandom(1, 49); // integer in
[1, 49]
function myRandom(a, b) { return a+Math.random()*(b-a);}
function myIntRandom(a, b) { return Math.floor(a+Math.random()*(b-a+1));}
// a random number in [a, b)
// an random integer in [a, b]
Date Object Constructors
new Date( )var today = New Date(); // Current date and time
new Date ( [ year [, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] ] ] )
var birthDay = new Date(1978, 0, 31); //Jan 31 00:00:00 1978
new Date (value)var day1970 = new Date(0); // Jan 1 00:00:00 1970
new Date(datestring)var someday = new Date("Mar 29 13:01 2009");
Date's Methods (1/3) Methods Description
getFullYear() the year, as a four-digit number
getMonth() the month (from 0-11)
getDate() the day of the month (from 1-31)
getDay() the day of the week (from 0-6)
getHours() the hour (from 0-23)
getMinutes() the minutes (from 0-59)
getSeconds() the seconds (from 0-59)
getMilliseconds() the milliseconds (from 0-999)
getTime() milliseconds since midnight Jan 1, 1970
getTimezoneOffset() Difference in minutes between local time and Greenwich Mean Time (GMT)
getUTCFullYear(), getUTCMonth(), getUTCDate(), getUTCDay(), getUTCHours(),
getUTCMinutes(), getUTCSeconds(), getUTCMilliseconds()
Date's Methods (2/3) Methods Description
setFullYear() the year, as a four-digit number
setMonth() the month (from 0-11)
setDate() the day of the month (from 1-31)
setHours() the hour (from 0-23)
setMinutes() the minutes (from 0-59)
setSeconds() the seconds (from 0-59)
setMilliseconds() the milliseconds (from 0-999)
setTime() milliseconds since midnight Jan 1, 1970
setUTCFullYear(), setUTCMonth(), setUTCDate(), setUTCHours(),
setUTCMinutes(), setUTCSeconds(), setUTCMilliseconds()
Date's Methods (3/3) Methods Description toString() Convert to a string
toDateString() The date portion in readable form
toTimeString() The time portion in readable form
toLocaleString() Convert to a string, according to local time
toLocaleDateString() Convert to a string, according to local time, and returns the date portion
toLocaleTimeString() Convert to a string, according to local time, and returns the time portion
toUTCString() Convert to a string, according to universal time
parse() Takes a date string and returns the number of milliseconds since midnight of January 1, 1970
UTC() Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time