JavaScript is...
자바 스크립트에 대해 살짝 훑어본 결과 느낀 건...
뭐야? 자바랑 다르다더니 완전 비슷하네? 물론 다르긴 하다만 기본적인 개념은 거의 비슷한듯?
일단 더 공부해 보자..
JavaScript Data Types
var answer2='He is called "Johnny"';
var pi=3.14;
var x=123;
var y=123e5;
var z=123e-5;
var cars=new Array("Saab","Volvo","BMW");
var person={firstname:"John", lastname:"Doe", id:5566};
JavaScript Arrays
The following code creates an Array called myCars:
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
or (condensed array):
or (literal array):
Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
JavaScript Objects
An object is delimited by curly braces. Inside the braces the object's properties are defined as name and
value pairs (name : value). The properties are separated by commas:
The object (person) in the example above has 3 properties: fistname, lastname, and id.
Spaces and line breaks are not important. Your declaration can span multiple lines:
firstname : "John",
lastname : "Doe",
id : 5566
};
You can address the object properties in two ways:
Null or Undefined
Non-existing is the value of a variable with no value.
Variables can be emptied by setting the value to null;
person=null;
Declaring Variable Types
When you declare a new variable, you can declare its type using the "new" keyword:
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;
Calling a Function with Arguments and Return Value
<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
var x=5;
return x;
}
</script>
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Operator | Description | Comparing | Returns | Try it |
---|---|---|---|---|
== | is equal to | x==8 | false | Try it » |
x==5 | true | Try it » | ||
=== | is exactly equal to (value and type) | x==="5" | false | Try it » |
x===5 | true | Try it » | ||
!= | is not equal | x!=8 | true | Try it » |
!== | is not equal (neither value or type) | x!=="5" | true | Try it » |
x!==5 | false | Try it » | ||
> | is greater than | x>8 | false | Try it » |
< | is less than | x<8 | true | Try it » |
>= | is greater than or equal to | x>=8 | false | Try it » |
<= | is less than or equal to | x<=8 | true | Try it » |
JavaScript Popup Boxes
자바스크립트에는 3가지 종류의 팝업 박스가 있다.
Alert Box
사용자에게 정보를 사용한다. "OK"버튼을 클릭해야 닫힌다.
Syntax
Confirm Box
사용자가 무엇인가를 확인하거나 수락해야 할때 사용한다.
"OK" 버튼이나 "Cancel" 버튼을 클릭해야 닫힌다. 버튼 클릭시 true, false값 반환
Syntax
Prompt Box
페이지에 들어가기전에 값을 입력받기위해 사용한다.
"OK" 버튼이나 "Cancel" 버튼을 클릭해야 닫힌다. 버튼 클릭시 입력값, null값 반환
Syntax
JavaScript For...In Statement
The for...in statement loops through the properties of an object.
이건 머 자바의 빠른 for문이랑 같은거네.. [ for( Integer x : 배열 ) ]
Syntax
{
code to be executed
}
Assign Events Using the HTML DOM
You can assign events to HTML elements within a script or a function:
Example
Assign an onclick event to a button element:
document.getElementById("myBtn").onclick=function(){displayDate()};
</script>
Try it yourself »
In the example above, a function named displayDate will be executed when the button is clicked.
JavaScript Special Characters
Code | Outputs |
---|---|
\' | single quote |
\" | double quote |
\\ | backslash |
\n | new line |
\r | carriage return |
\t | tab |
\b | backspace |
\f | form feed |
'Dev. 웹 > JavaScript' 카테고리의 다른 글
[JavaScript 예제] 시간이 흐르는 시간 텍스트 출력하기 (0) | 2012.09.12 |
---|---|
[JavaScript 예제] 버튼 클릭시 구구단 출력 (0) | 2012.09.12 |
[JavaScript 예제] 버튼 클릭 이벤트, div클릭시 숫자 증가 보여주기 (0) | 2012.09.12 |
jquery 기초 정리 및 예제 (1) | 2012.09.11 |
자바스크립트(JavaScript) 작성 가이드라인 (0) | 2012.09.11 |