24
JavaScript Functions & Objects

JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

Embed Size (px)

Citation preview

Page 1: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

JavaScript Functions & Objects

Page 2: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

JavaScript Global 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

Number() Converts an object's value to a 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()

Page 3: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

encodeURI(), decodeURI()var uUri = "http://www.google.com.tw/search?q= 暨南大學 ";var eUri = encodeURI(uUri);document.write(uUri + "<br />");document.write(eUri + "<br />");document.write(decodeURI(eUri) + "<br />");

http://www.google.com.tw/search?q= 暨南大學http://www.google.com.tw/search?q=%E6%9A%A8%E5%8D%97%E5%A4%A7%E5%AD%B8http://www.google.com.tw/search?q= 暨南大學

Page 4: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

isNaN()<head><script type="text/javascript">window.onload = function() { document.getElementById(ageBtn).onclick=chkAge;};function chkAge() { var age = document.getElementById(age).value; if (isNaN(age)) { alert("年齡輸入錯誤 !"); // ... }}</script></head><body><form …>Age: <input type="text" id="age" /><input type="button" id="ageBtn" value="Check"/> <br/></form></body>

http://ycchen.im.ncnu.edu.tw/www2011/lab/isNaN.html

Page 5: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

JavaScript Objects

Boolean Number String Array Math Date RegExp

Page 6: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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");

Page 7: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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( )

var num = 5000; var n2E = num.toExponential(2); //5.00e+3var numObj = 6000;var o2E= numObj.toExponential(1); //6.0e+3var num2 = 3.456;var n2F = num2.toFixed(1); //3.5var n2P = num2.toPrecision(3); //3.46

Page 8: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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)

Page 9: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

indexOf()

var email ="[email protected]";posAt = email.indexOf("@");if (posAt == -1) alert("Wrong E-mail!");else {

user = email.substring(0, posAt);serv = email.substring(posAt+1, email.length);

alert("User name: "+user+"\nMail Server: "+serv);

}http://ycchen.im.ncnu.edu.tw/www2011/lab/indexOf.html

Page 10: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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

Page 11: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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"

Page 12: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

Array Objectvar arr1 = new Array();var arr2 = new Array(4);var arr3 = new Array(2009, "April", true); Properties:

lengthlength 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()

Page 13: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

length

var arr1=new Array();var sum=0;arr1[0]= 32;arr1[1]=75;// …for (var i=0; i<arr1.length; i++) { sum += arr1[i]; // …}

Page 14: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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

Page 15: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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,1000

function sortByNum(a, b) { return a-b;}

Page 16: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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

Page 17: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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)

Page 18: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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

Page 19: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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]

Page 20: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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");

Page 21: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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()

Page 22: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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()

Page 23: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

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

Page 24: JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded

Date Examples

Date methodshttp://ycchen.im.ncnu.edu.tw/www2011/lab/

date.html

日期時間計算http://ycchen.im.ncnu.edu.tw/www2011/lab/

date1.html