1 / 50

JavaScript – valodas sintakse

Web aplikāciju tehnoloģijas. JavaScript – valodas sintakse. Tipi. #1. Bāzes tipi. Number 0.3, 12 Boolean true, false String ‘hello’, “rinda” null undefined nav vērtības. Objekti. Objekti Array String Date Function. Bāzes tipi. Deklarācija: var orange=“ apelsīns ” Objekti:

ziya
Download Presentation

JavaScript – valodas sintakse

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. Web aplikāciju tehnoloģijas JavaScript – valodas sintakse

  2. Tipi #1.

  3. Bāzes tipi • Number • 0.3, 12 • Boolean • true, false • String • ‘hello’, “rinda” • null • undefined • nav vērtības

  4. Objekti • Objekti • Array • String • Date • Function • ...

  5. Bāzes tipi • Deklarācija: • var orange=“apelsīns” • Objekti: • var orange = new String(“apelsīns”) • var ok = new Boolean(true) // nav labi • alert(“apelsīns”.lenght) • alert(typeof "test“) - string • alert(typeofnew String("test")) - object

  6. Numbers

  7. Numbers – neprecīzi aprēķini • alert(0.1+0.2) // rezultāts nebūs 0.3! • Formāts – float64

  8. Kļūdu maskēšana • 1/0 = Number.POSITIVE_INFINITY (+∞) • -1/0 = Number.NEGATIVE_INFINITY • Number(“not_a_number”) • =NaN (Not-a-Number, nav numurs)

  9. Dažas noderīgas metodes

  10. Noapaļošana • 0.1234.toPrecision(2) = 0.12

  11. Konvertēšana uz skaitli • var str = "002” • var a = Number(str)// 2 • +"0.1" // => 0.1 • +"0.1z" // => Number.NaN

  12. Konvertēšana uz skaitli • // viss pēc skaitļa tiek ignorēts • parseFloat("0.1zf") = 0.1 • parseInt("08f.4", 10) = 8 • // šeit nav skaitļa • parseFloat("smth") = Number.NaN

  13. Konvertēšana uz skaitli • parseInt("0x10") = 16 • parseInt("010") = 8 • parseInt("010",10) = 10

  14. Matemātiskas funkcijas • Math.floor() / Math.round() / Math.ceil() - noapaļošana • Math.abs() – skaitļa modulis • Math.sin() – sinuss • u.t.t.

  15. String • Visas rindas – ar Unicode kodējumu • ‘string’ – ok • “string” – ok • “zvaigzne: \u002a” • “hello world”.replace(/(.*?)\s(.*)/, "$2, $1!") • “hello world”.lenght

  16. Boolean • Aplams: • False • Null • Undefined • “” • 0 • Number.NaN • Patiess: • все остальное • “0” • “false”

  17. Konvertēšana uz loģisko tipu • Boolean(a) • !!a

  18. JavaScript operatori

  19. Arifmetiski • + (arī konkatenācija) • - • * • / • %

  20. Konkatenācija • alert(‘1’+1) • alert(1+’1’) • alert(1+1) • alert(1+true)

  21. Bitu operācijas • & • | • ^ • >> • >>> • <<

  22. Loģiskas • && • || • !

  23. Salidzināšanas • == • != • < • > • <= • >= • === • !== • ?:

  24. Masīvi

  25. Skaitliskais masīvs (objekti) • Asociatīvais masīvs

  26. Masīva izveidošana • var a = new Array() • var a = [] • var a = new Array("a", 1, true) • var a = ["a", 1, true] • var a = [] • a[1] = 1 • a[999999] = 2

  27. Masīva izmērs • a.length • a[a.length] = "new element” • a.push("new element")

  28. Masīva steks • vararr = [3,5,7] • arr.push(9) • var last = arr.pop()//= 9 • var last = arr.pop() // = 7 • alert(arr.length) // = 2 • vararr = [4,6,8] • arr.unshift(2) // arr = [2,4,6,8] • arr.unshift(0) // arr = [0,2,4,6,8] • var last = arr.shift() // last = 0, arr = [2,4,6,8] • arr.shift() // arr = [4,6,8]

  29. Citas metodes • slice(begin[, end]) – apakšmasīvs • splice(index, deleteCount[, element1,…, elementN]) – izdzēst daļu no masīva un aizvietot ar jaunām vērtībām • join • reverse • ...

  30. Funkcijas

  31. Funkciju izveidošana • FunctionDeclaration (ar vārdu) • Function vārds(parametri){ ...} • var a = sum(2,2)function sum(x,y) { return x+y }

  32. Funkciju izveidošana • FunctionExpression (anonīmas) • var имя = function(parametri){…}...var vārds = new Function(parametri, '...') • var sum = function(x,y) { return x+y } var a = sum(2,2)

  33. Funkcijas-objekti • function f() { ... }f.test = 6…alert(f.test) // 6 • function func(){varfuncObj = arguments.calleefuncObj.test++alert(funcObj.test)}func.test = 1 func() func()

  34. Funkcijas parametri • var run = function(distance, speed) {speed = speed || 10 var time = distance / speed return time} • run(10) => 1 • run(10, 2) => 5 • run(10, 2, 5, 6, 7) => 5 • run() => undefined / 10 = NaN

  35. Parametri • arguments – parametru objekts • arguments.lenght • arguments.[...] • arguments.callee

  36. Valodas konstrukcijas

  37. Cikls while • while(i<5) { … } • do { … } while (i<5)

  38. Cikls for • for (vari=0;i<10;i++){ ... } • for(key in obj){ ... obj[key] } • // pārbaudam tikai savas īpašībasfor(var key in obj){if (!obj.hasOwnProperty(key)){ … obj[key] … }}

  39. Atzīmes • outer:for(…){ …for (…){ … break outer;…continue outer; ...}}

  40. switch konstrukcija • switch (obj){case "test“:…breakcase 5:…break outerdefault:…}

  41. Izņēmumi (exceptions) • try{…throw {message: “Paziņojums“}...}catch (e){alert(“Apstrāde”)}

  42. Klase Error • alert( (new Error()).stack )

  43. finally • ...finally{ // izpildīsies vienmēr}

  44. JavaScript klienta pamata objekti

  45. Hierarhija

  46. Tīmekļa lapas objekti • navigator • window • document • location • history • document.myform.text1.value • document.imageForm.aircraft.src='f15e.gif' • document.aircraft.src='f15e.gif'

  47. window objekts • alert () • cofirm () • prompt () • blur () • focus () • scroll () • setTimeout() • location • status

  48. Form • document.forms(x) • document.forms(x).elements(x)

  49. Logu pārvaldīšana • window.open() • window.close() • <window_name>.<...>

  50. Patstāvīgais uzdevums: • JavaScript objekti (hash) • AJAX • ECMA-262 standarts

More Related