1 / 20

JavaScript Functions & Objects

JavaScript Functions & Objects. JavaScript Functions. escape(), unescape(). http://www.google.com.tw/search?q= 暨南大學 escape: http%3A//www.google.com.tw/search%3Fq%3D%u66A8%u5357%u5927%u5B78 %u570B%u9053%u516D%u865F unescape: 國道六號. var uUrl = "http://www.google.com.tw/search?q= 暨南大學 ";

zanthe
Download Presentation

JavaScript Functions & Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript Functions & Objects

  2. JavaScript Functions

  3. escape(), unescape() 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: 國道六號 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 />");

  4. JavaScript Objects Boolean Number String Array Math Date RegExp

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

  6. Number Object num = 5000 num.toExponential(2) = 5.00e+3numObj = 6000numObj.toExponential(1) = 6.0e+3num2 = 3.456num2.toFixed(1) = 3.5num2.toPrecision(3) = 3.46 varmyNum=new Number(86); • Properties: • MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY • Methods: • toExponential(num) • toFixed(num) • toPrecision(num) • toString( )

  7. String Object var 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)

  8. slice( ) vs. substring( ) index  (length-index)  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"

  9. 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-typemultipart/mixed; boundary="----xxyy"

  10. Array Object splice() pop() shift() push() unshift() var 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)

  11. splice(index, howmany, element1, ..., elementX) // 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 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 />");

  12. 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,1 document.write(arr.sort()+ "<br />"); // 1,10,1000,25,40,5 document.write(arr.sort(sortByNum)+ "<br />"); // 1,5,10,25,40,1000 function sortByNum(a, b) { return a-b; }

  13. join(separator) JavaScript,CSS,XML,Dynamic HTMLJavaScript, CSS, XML, Dynamic HTML var arr1 = ["JavaScript", "CSS", "XML", "Dynamic HTML"]; var str1 = arr1.join(); var str2 = arr1.join(", "); document.write(str1+"<br />"); document.write(str2+"<br />");

  14. Math Object • Math is a static object. var area = 2*2* Math.PI; • Properties:

  15. Math's Methods

  16. Math.random() // a random number in [a, b) // an random integer in [a, b] 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)); }

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

  18. Date's Methods (1/3) getUTCFullYear(), getUTCMonth(), getUTCDate(), getUTCDay(), getUTCHours(), getUTCMinutes(), getUTCSeconds(), getUTCMilliseconds()

  19. Date's Methods (2/3) setUTCFullYear(), setUTCMonth(), setUTCDate(), setUTCHours(), setUTCMinutes(), setUTCSeconds(), setUTCMilliseconds()

  20. Date's Methods (3/3)

More Related