try-catch-throw, special text

ITWeb/개발일반 2008. 2. 20. 16:30

 
Events
Try…Catch
try {
} catch (err) {
// err.name, err.message
// err.number, err.description
}
Throw
try {
throw "error1";
} catch (err) {
if ( err == "error1" ) {
alert("error1 발생");
}
}


 
JavaScript Special Text
 
Code
Outputs
\'
single quote
\"
double quote
\&
ampersand
\\
backslash
\n
new line
\r
carriage return
\t
tab
\b
backspace
\f
form feed


:

JavaScript Loops

ITWeb/개발일반 2008. 2. 20. 16:28

 
JavaScript Loops
for
§for (var=startvalue;var<=endvalue;var=var+increment) {
§ code to be executed
§}
§for (variable in object) {
§ code to be executed
§}
while
§while (var<=endvalue) {
§ code to be executed
§}
§do {
§ code to be executed
§} while (var<=endvalue);

 
break / continue (break label / continue label)
§for (i=0;i<=10;i++) {
§ if (i==3) {
§ break;
§ }
§ document.write("The number is " + I + "<br />");
§}
§forLable : for (i=0;i<=10;i++) {
§ if (i==3) {
§ continue forLable;
§ }
§ document.write("The number is " + I + "<br />");
§}
 
with
§with ( document ) { // 반복 시킬 객체
§ write(생략된 객체 반복1<br>); // 구문
§ write(생략된 객체 반복2<br>); // 구문
§}
:

JavaScript Functions

ITWeb/개발일반 2008. 2. 20. 16:22

 
JavaScript Functions
-Functions are first-class objects
-Functions can be passed, returned, and stored just like any other value
-Functions inherit from Object and can store name/value pairs.
-A function is a reusable code-block that will be executed by an event, or when the function is called.
§function functionname(var1,var2,...,varX) {
§ some code
§ or
§ some code
§ return RETURNVALUE;
§}
-function foo() {} expands to var foo = function foo() {}

Inner Functions
-Functions do not all have to be defined at the top level (or left edge).
-Functions can be defined inside of other functions.
-Scope
§    An inner function has access to the variables and parameters of functions that it is contained within.
§    This is known as Static Scoping or Lexical Scoping.
:

if else statement, switch statement, operators

ITWeb/개발일반 2008. 2. 20. 16:12
 
JavaScript If…Else Statements
if ( CONDITION ) {
}
if ( CONDITION ) {
} else {
}
if ( CONDITION ) {
} else if ( CONDITION ) {
}
if ( CONDITION ) {
} else if ( CONDITION ) {
} else {
}


 
JavaScript Switch Statement
switch(n) {
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is
different from case 1 and 2
}

JavaScript Operators
 
Operator
Description
Example
Result
+
Addition
x=2
y=2
x+y
4
-
Subtraction
x=5
y=2
x-y
3
*
Multiplication
x=5
y=4
x*y
20
/
Division
15/5
5/2
3
2.5
%
Modulus (division remainder)
5%2
10%8
10%2
1
2
0
++
Increment
x=5
x++
x=6
--
Decrement
x=5
x--
x=4
 
Operator
Example
Is The Same As
=
x=y
x=y
+=
x+=y
x=x+y
-=
x-=y
x=x-y
*=
x*=y
x=x*y
/=
x/=y
x=x/y
%=
x%=y
x=x%y
 
Operator
Description
Example
==
is equal to
5==8 returns false
===
is equal to (checks for both value and type)
x=5
y="5"
x==y returns true
x===y returns false
!=
is not equal
5!=8 returns true
>
is greater than
5>8 returns false
<
is less than
5<8 returns true
>=
is greater than or equal to
5>=8 returns false
<=
is less than or equal to
5<=8 returns true

 
-&&
§    If first operand is truthy
        then result is second operand
            else result is first operand
-||
§    If first operand is truthy
        then result is first operand
            else result is second operand
Operator
Description
Example
&&
and
x=6
y=3
(x < 10 && y > 1) returns true
||
or
x=6
y=3
(x==5 || y==5) returns false
!
not
x=6
y=3
!(x==y) returns true
 
-String
§txt1="What a very";
§txt2="nice day!";
§txt3=txt1+txt2;
-Conditional
§variablename = (condition) ? value1 : value2;

 
-alert
§var result = alert("sometext");
§alert(result); // undefined
-confirm
§var result = confirm("sometext");
§alert(result); // 확인:true, 취소:false
-prompt
§var result = prompt("sometext", "defaultvalue");
§alert(result); // 확인:string, 취소:null
:

block, comments, variables

ITWeb/개발일반 2008. 2. 20. 15:57
 
JavaScript Blocks
{}
-JavaScript does not have block scope.
-Blocks should only be used with structured statements
§function, if, switch, while, for, do, try
JavaScript Comments
-Single-Line comments : //
-Multi-Line comments : /*…*/
JavaScript Variables
-Variables are "containers" for storing information.
-var keyword is defined local variable.
: