You are on page 1of 25

Roteiro Try...

Catch Throw Caracteres especiais Introduo a objetos

Manipulao do cdigo JS
Prof. Adriano Cunha
Sistemas para Internet

10 de setembro de 2012

Cunha (SENAC)

Linguagem de Script

AGO/2012

1 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Roteiro
1
Try...Catch Conceitos Exemplos

Throw Conceitos Exemplos

3 4

Caracteres especiais

Introduo a objetos String Date Math RegExp Criados pelo usurio

Cunha (SENAC)

Linguagem de Script

AGO/2012

2 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Permite a realizao de testes em blocos de cdigo, possibilitando o gerenciamento dos erros. O bloco Try contm o cdigo a ser executado, e o bloco Catch contm o bloco a ser executado em caso de erros. Sintaxe try { //Run some code here } catch(err) { //Handle errors here }
Cunha (SENAC) Linguagem de Script AGO/2012 3 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Exemplo <!DOCTYPE html> <html> <head> <script> var txt= ; function message(){ try{adddlert(Welcome guest!);} catch(err){ txt=There was an error on this page.; txt+=Error description: + err.message; txt+=Click OK to continue.; alert(txt); } } </script> Cunha (SENAC) Linguagem de Script

AGO/2012

4 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Exemplo <body> <input type=button value=View message onclick=message() /> </body> </html>

Cunha (SENAC)

Linguagem de Script

AGO/2012

5 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Permite a criao de excees. Pode ser utilizado junto com try...catch para controlar o uxo do programa e gerar mensagens de erros mais precisas. Sintaxe throw exception;

Cunha (SENAC)

Linguagem de Script

AGO/2012

6 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Exemplo <script> var x=prompt(Enter a number between 5 and 10:, ); try { if(x>10) { throw Err1; } else if(x<5) { throw Err2; } else if(isNaN(x)) { throw Err3; Cunha (SENAC) Linguagem de Script

AGO/2012

7 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Conceitos Exemplos

Exemplo catch(err) { if(err==Err1) { document.write(Error! The value is too high.); } if(err==Err2) { document.write(Error! The value is too low.); } if(err==Err3) { document.write(Error! The value is not a number.); } } </script> Cunha (SENAC) Linguagem de Script

AGO/2012

8 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

Pode-se utilizar a barra invertida para adicionar caracteres especiais em um texto.

Cunha (SENAC)

Linguagem de Script

AGO/2012

9 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

O Js uma linguagem de programao baseada em objetos, que permite ao usurio utilizar os objetos denidos pelo sistema ou criar seus prprios objetos e tipos de variveis. Um objeto composto por propriedades e mtodos. As propriedades so os valores associados a um objeto e os mtodos so as aes associados ao mesmo.

Cunha (SENAC)

Linguagem de Script

AGO/2012

10 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Este objeto usado para manipular pedaos de texto armazenados. Exemplo var txt=Hello world!; document.write(txt.length); Exemplo var txt=Hello world!; document.write(txt.toUpperCase());

Cunha (SENAC)

Linguagem de Script

AGO/2012

11 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Este objeto usado para trabalhar com datas e horas. So criados com o uso do construtor Date(). Existem quatro formas de iniciar uma data: new Date(); // var today = new Date(); new Date(milliseconds); // var d1 = new Date(October 13, 1975 11:13:00); new Date(dateString); // var d2 = new Date(79,5,24); new Date(year,month,day,hours,minutes,seconds,milliseconds); // var d3 = new Date(79,5,24,11,33,0);

Cunha (SENAC)

Linguagem de Script

AGO/2012

12 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Exemplos:
var myDate=new Date(); myDate.setFullYear(2010,0,14); var myDate=new Date(); myDate.setDate(myDate.getDate()+5); var x=new Date(); x.setFullYear(2100,0,14); var today = new Date(); if (x>today) {alert(Today is before 14th January 2100);} else {alert(Today is after 14th January 2100);}
Cunha (SENAC) Linguagem de Script AGO/2012 13 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Permite a realizao de tarefas matemticas. Inclui vrias constantes e mtodos matemticos. Exemplos: var x=Math.PI; var y=Math.sqrt(16); Math.E Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E
Cunha (SENAC) Linguagem de Script AGO/2012 14 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Exemplos: document.write(Math.round(4.7)); document.write(Math.random()); document.write(Math.oor(Math.random()*11));

Cunha (SENAC)

Linguagem de Script

AGO/2012

15 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Conceitos

O objeto RegExp utilizado para descrever padres de caracteres. Os padres podem ser utilizados para realizar buscas em textos. A existncia de um simples padro pode ser utilizado para representar um padro. Os padres podem ser utilizados em vrias tarefas, como: vericaes, validaes, substituies, transformaes, etc.

Cunha (SENAC)

Linguagem de Script

AGO/2012

16 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Conceitos

Sintaxe var patt = new RegExp(pattern, modier); ou var patt = /pattern/modier; Modier Usados para realizar buscas globais (g) e/ou case-insensitive (i).

Cunha (SENAC)

Linguagem de Script

AGO/2012

17 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Exemplos
<!DOCTYPE> <html> ...<body> ......<script> ........var str = Is this all there is?; ........var pattern = /This/i; ........document.write(str.match(pattern)); ......</script> ...</body> </html>

Cunha (SENAC)

Linguagem de Script

AGO/2012

18 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Exemplos
<!DOCTYPE> <html> ...<body> ......<script> ........var str = Is this all there is?; ........var pattern = /is/g; ........document.write(str.match(pattern)); ......</script> ...</body> </html>

Cunha (SENAC)

Linguagem de Script

AGO/2012

19 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Exemplos
<!DOCTYPE> <html> ...<body> ......<script> ........var str = Is this all there is?; ........var pattern = /is/gi; ........document.write(str.match(pattern)); ......</script> ...</body> </html>

Cunha (SENAC)

Linguagem de Script

AGO/2012

20 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Mtodo Test()

Procura em uma string um determinado valor e retorna verdadeiro ou falso. ... var pattern = new RegExp(e); document.write(pattern.test(Is this all there is?)); ...

Cunha (SENAC)

Linguagem de Script

AGO/2012

21 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Mtodo Exec()

Procura em uma string um determinado valor. Caso o encontre, retorna o valor achado, e se no achar, retorna null. ... var pattern = new RegExp(e); document.write(pattern.exec(Is this all there is?)); ...

Cunha (SENAC)

Linguagem de Script

AGO/2012

22 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Criados diretamente pelo usurio


personObj=new Object(); personObj.rstname=Fulano; personObj.lastname=de tal; personObj.age=50; personObj.eyecolor=Preto; personObj=rstname:Fulano, lastname:de tal, age:50, eyecolor:Preto; personObj.eat=eat;

Cunha (SENAC)

Linguagem de Script

AGO/2012

23 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Criados atravs de construtores


function person(rstname,lastname,age,eyecolor) { this.rstname=rstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; } var myFather=new person(John,Doe,50,blue); var myMother=new person(Sally,Rally,48,green);

Cunha (SENAC)

Linguagem de Script

AGO/2012

24 / 25

Roteiro Try...Catch Throw Caracteres especiais Introduo a objetos

String Date Math RegExp Criados pelo usurio

Criados atravs de construtores


function person(rstname,lastname,age,eyecolor) { this.rstname=rstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.newlastname=newlastname; } function newlastname(new_lastname) { this.lastname=new_lastname; }
Cunha (SENAC) Linguagem de Script AGO/2012 25 / 25

You might also like