βΆ ν¨μ
*κΈ°λ³Έ ν¨μ μ μλ¬Έ
ν¨μλ₯Ό μ¬μ©νμ¬ μ½λλ₯Ό μ μ₯ν κ²μ 'ν¨μ μ μλ¬Έ'μ΄λΌκ³ νλ€.
ν¨μμμλ function ν€μλλ₯Ό μ¬μ©ν΄ λ³μλ₯Ό μ μΈνλ€.
function ν¨μλͺ
() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
λ€μκ³Ό κ°μ΄ μ΅λͺ ν¨μ(ν¨μλͺ μ΄ μλ ν¨μ)λ₯Ό μ μΈνκ³ λ³μμ μ°Έμ‘°ν΄λ λλ€.
μ°Έμ‘° λ³μ = function() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
ν¨μ μ μλ¬Έ({...}) μμ μμ±λ μ½λλ μ¦μ μ€νλμ§ μλλ€. ν¨μλ λ©λͺ¨λ¦¬μ ν λΉλμ΄ λκΈ°νκ³ μλ€κ° ν¨μκ° νΈμΆλλ©΄ μ€νλλ€. μ μλμ΄ μλ ν¨μλ₯Ό νΈμΆνλ κΈ°λ³Ένμ λ€μκ³Ό κ°λ€.
ν¨μλͺ
();
λλ μ°Έμ‘° λ³μ();
β μΌλ° ν¨μ μ μλ ν¨μ νΈμΆ μ νΈμ΄μ€ν (hoisting) κΈ°μ μ μ§μνλ€. κ·Έλ¬λ μ΅λͺ ν¨μ μ μΈ μ°Έμ‘° λ°©μμ νΈμ΄μ€ν μ μ§μνμ§ μλλ€. νΈμ΄μ€ν μ μ μ©νλ©΄ ν¨μ μ μλ¬Έλ³΄λ€ νΈμΆλ¬Έμ΄ λ¨Όμ λμλ ν¨μ μ μλ¬Έμ λμ΄μ¬λ € ν¨μλ₯Ό νΈμΆνλ€.
μμ ) ν¨μ μ μλ¬Έκ³Ό μ΅λͺ ν¨μλ₯Ό λ³μμ μ°Έμ‘°ν μ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
var count = 0;
myFnc();
function myFnc() {
count++;
document.write("hello" + count,"<br>");
}
myFnc();
var theFnc = function() {
count++;
document.write("bye" + count,"<br>");
}
theFnc();
</script>
</head>
<body>
</body>
</html>
μμ 2) λ°°κ²½μ λ°κΎΈκΈ°
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
var color=["white", "yellow", "aqua", "purple"];
var i=0;
function changeColor() {
i++;
if(i >= color.length) {
i = 0;
}
var bodyTag = document.getElementById("theBody");
bodyTag.style.backgroundColor = color[i];
}
</script>
</head>
<body id="theBody">
<button onclick="changeColor();">λ°°κ²½μ λ°κΎΈκΈ°</button>
</body>
</html>
*맀κ°λ³μκ° μλ ν¨μ μ μλ¬Έ
맀κ°λ³μκ° μλ ν¨μ μ μλ¬Έμ ν¨μλ₯Ό νΈμΆν λ μ λ¬νκ³ μ νλ κ°μ μ λ ₯νμ¬ νΈμΆν μ μλ€.
μ΄λ κ² μ λ¬λ κ°μ 맀κ°λ³μκ° λ°μ ν¨μ μ μλ¬Έμμ μ¬μ©ν μ μκ² λλ€.
function ν¨μλͺ
(맀κ°λ³μ 1, 맀κ°λ³μ 2,... 맀κ°λ³μ n) {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
ν¨μλͺ
(λ°μ΄ν° 1, λ°μ΄ν° 2,... λ°μ΄ν° n);
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
function myFnc(name, area) {
document.write("μλ
νμΈμ. " + name + "μ
λλ€.", "<br>");
document.write("μ¬λκ³³μ " + area + "μ
λλ€.", "<br><br>");
}
myFnc("νλΉλ¬΄", "μμΈ");
myFnc("κΉλκΈ°", "λΆμ°");
</script>
</head>
<body>
</body>
</html>
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
var rightId = "korea";
var rightPw = "1234";
function login(id, pw){
if(id == rightId){
if(pw == rightPw){
document.write(id + "λ λ°©λ¬Έμ νμν©λλ€");
}else{
alert("μλͺ»λ λΉλ°λ²νΈμ
λλ€.");
}
} else {
alert("μ‘΄μ¬νμ§ μλ μμ΄λμ
λλ€.");
}
}
var userId = prompt("μμ΄λλ₯Ό μ
λ ₯νμΈμ.", "");
var userPw = prompt("ν¨μ€μλλ₯Ό μ
λ ₯νμΈμ.", "");
login(userId, userPw);
</script>
</head>
<body>
</body>
</html>
β
β
*맀κ°λ³μ μμ΄ ν¨μμ μ λ¬λ κ° λ°μμ€κΈ°
ν¨μ μ μλ¬Έμμ argumentsμ μ¬μ©νλ©΄ 맀κ°λ³μλ₯Ό μ¬μ©νλ κ²μ²λΌ ν¨μ νΈμΆλ¬Έμ κ°μ λ°μμ¬ μ μλ€. ν¨μ μ μλ¬Έμ 맀κ°λ³μκ° μλ μνμμ λ°μ΄ν°λ₯Ό μ λ¬νμ¬ ν¨μλ₯Ό νΈμΆνλ©΄ κ·Έ κ°μ λ°°μ΄μ μ μ₯λλ€. ν¨μ μ μλ¬Έμμλ κ·Έ κ°μ argumentsλΌλ λ³μλ‘ μ°Έμ‘°νλ€.
β
βΆ ν¨μμμ return λ¬Έμ μν
return λ¬Έμ ν¨μμμ κ²°κ΄κ°μ λ°νν λ μ¬μ©νλ€. κ·Έλ¦¬κ³ ν¨μμμ return λ¬Έμ΄ μ€νλλ©΄ λ°λ³΅λ¬Έμ break λ¬Έκ³Ό λΉμ·νκ² μ½λκ° κ°μ λ‘ μ’ λ£λλ€. λ€μ λ§ν΄ ν¨μ μ μλ¬Έμ return λ¬Έμ΄ μ¬μ©λλ©΄ ν¨μλ₯Ό νΈμΆνμ λ κ²°κ΄κ°(data)μ λ°ννλ€.
β
*λ°μ΄ν°λ₯Ό λ°ννκ³ κ°μ μ’ λ£νλ returnλ¬Έ
function ν¨μλͺ
() {
μλ°μ€ν¬λ¦½νΈ μ½λ1;
return λ°μ΄ν°(κ°);
μλ°μ€ν¬λ¦½νΈ μ½λ2;
}
var λ³μ = ν¨μλͺ
();
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
function testAvg(arrData){
var sum = 0;
for(var i = 0; i < arrData.length; i++) {
sum += Number(prompt(arrData[i] + " μ μλ?", "0"));
}
var avg = sum / arrData.length;
return avg;
}
var arrSubject = ["κ΅μ΄", "μν"];
var result = testAvg(arrSubject);
document.write("νκ· μ μλ " + result + "μ μ
λλ€");
</script>
</head>
<body>
</body>
</html>
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<style>
*{margin: 0;padding: 0;}
#galleryZone {text-align: center;}
#galleryZone input[type="image"]{margin:0 8px;}
</style>
<script>
var num = 1;
function gallery(direct) {
if(direct) {
if(num == 8) return;
num++;
} else {
if(num == 1) return;
num--;
}
var imgTag = document.getElementById("photo");
imgTag.setAttribute("src","images/pic_" + num + ".jpg");
}
</script>
</head>
<body>
<div id="galleryZone">
<p><img src="images/pic_1.jpg" id="photo" alt=""></p>
<p>
<button onclick="gallery(0)">μ΄μ </button>
<button onclick="gallery(1)">λ€μ</button>
</p>
</div>
</body>
</html>
βΆ μ¬κ· ν¨μ νΈμΆ
function myFnc() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
myFnc();
}
myFnc();
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
var num = 0;
function testFnc(){
num++;
document.write(num, "<br>");
if(num == 10) return;
testFnc();
}
testFnc();
</script>
</head>
<body>
</body>
</html>
βΆ ν¨μ μ€μ½ν κ°λ μ΄ν΄
*ν¨μ μ€μ½νλ?
μ€μ½ν(Scope)μ μ¬μ μ μλ―Έλ 'λ²μ'μ΄λ©°, μ¬κΈ°μμλ λ³μ λλ ν¨μμ μ ν¨ λ²μλ₯Ό κ°λ¦¬ν¨λ€.
β
β
*μ μ λ³μμ μ§μ λ³μμ κ°λ κ³Ό μ°¨μ΄
μ μ λ³μλ μλ°μ€ν¬λ¦½νΈ μ΄λμμλ μ¬μ©ν μ μλ λ³μμ΄κ³ μ§μ λ³μλ ν¨μ μ€μ½νμμλ§ μ¬μ©ν μ μλ λ³μμ΄λ€. λ€μμ μ μ λ³μμ μ§μ λ³μλ₯Ό μ μΈνλ κΈ°λ³Ένμ΄λ€.
β
β
*μ μ ν¨μμ μ§μ ν¨μμ μ°¨μ΄
μ μ ν¨μλ μλ°μ€ν¬λ¦½νΈ μ΄λμμλ μ¬μ©ν μ μλ ν¨μμ΄κ³ , μ§μ ν¨μλ ν¨μ μ€μ½νμμλ§ μ¬μ©ν μ μλ ν¨μμ΄λ€. λ€μμ μ μ ν¨μμ μ§μ ν¨μλ₯Ό μ μΈνλ κΈ°λ³Ένμ΄λ€.
function ν¨μλͺ
1() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
function ν¨μλͺ
2() {
function ν¨μλͺ
3() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
}
ν¨μ μ€μ½νμμ μ μΈλ ν¨μλ μ§μ ν¨μκ° λμ΄ ν¨μ μ€μ½νμμλ§ νΈμΆν μ μλ€. λ°λΌμ μ€μ½ν λ°μμ myFnc() ν¨μλ₯Ό νΈμΆνλ©΄ ν¨μ μ€μ½νμμ μ μΈν μ§μ ν¨μκ° μλ μ μ ν¨μκ° μ€νλμ΄ 'μ μ ν¨μ'λΌλ κ²½κ³ μ°½μ΄ λνλλ€.
βΆ μ μκ³Ό μ§μμ λλλ μ΄μ
νλ‘κ·Έλ¨μ κ°λ°ν λ μ μ(Global)κ³Ό μ§μ(Local)μ λλλ©΄ μΆ©λμ νΌν μ μλ€.
β
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
var num = 100;
function menu( ) {
num += 100;
alert(num);
}
menu();
function menu( ) {
alert(num);
}
</script>
</head>
<body>
</body>
</html>
-μ¦μ μ€ν ν¨μ
μ§μν¨μλ₯Ό μ¬μ©νμ¬ ν¨μλ₯Ό μ μΈνλ©΄ ν¨μκ° μΆ©λνλ λΆμμ¬λ₯Ό νΌν μ μλ€.
(function() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}());
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
(function() {
var num = 100;
function menu( ) {
num += 100;
alert(num);
}
menu( );
}());
(function(){
var num = 100;
function menu( ) {
alert(num);
}
}( ));
</script>
</head>
<body>
</body>
</html>
βΆ κ°μ²΄ μμ±μ ν¨μμ νμ©
*κ°μ²΄ μμ±μ ν¨μ
λ΄μ₯ κ°μ²΄λ₯Ό μμ±ν λλ μ΄λ―Έ μλ°μ€ν¬λ¦½νΈ μμ§μ λ΄μ₯λμ΄ μλ κ°μ²΄ μμ±μ ν¨μλ₯Ό μ¬μ©νμ¬ κ°μ²΄λ₯Ό μμ±νλ€.
function ν¨μλͺ
(맀κ°λ³μ 1, 맀κ°λ³μ 2,... 맀κ°λ³μ n) {
this.μμ±λͺ
= μ κ°;
this.ν¨μλͺ
= function() {
μλ°μ€ν¬λ¦½νΈ μ½λ;
}
}
var μ°Έμ‘° λ³μ(μΈμ€ν΄μ€ λ€μ) = new ν¨μλͺ
();
↓
var μ°Έμ‘° λ³μ = { μμ±: μ κ°, ν¨μλͺ
: function() { ... } }
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
function CheckWeight(name, height, weight) {
this.userName = name;
this.userHeight = height;
this.userWeight = weight;
this.minWeight;
this.maxWeight;
this.getInfo = function() {
var str = ""
str += "μ΄λ¦: " + this.userName + ", ";
str += "ν€: " + this.userHeight + ", ";
str += "λͺΈλ¬΄κ²: " + this.userWeight + "<br>";
return str;
}
this.getResult = function( ) {
this.minWeight = (this.userHeight - 100) * 0.9 - 5;
this.maxWeight = (this.userHeight - 100) * 0.9 + 5;
if(this.userWeight >= this.minWeight &&
this.userWeight <= this.maxWeight) {
return "μ μ λͺΈλ¬΄κ²μ
λλ€";
} else if(this.userWeight < this.minWeight) {
return "μ μ λͺΈλ¬΄κ²λ³΄λ€ λ―Έλ¬μ
λλ€";
} else {
return "μ μ λͺΈλ¬΄κ²λ³΄λ€ μ΄κ³Όμ
λλ€";
}
}
}
var jang = new CheckWeight("μ₯보리", 168, 62);
var park = new CheckWeight("λ°λ¬μ¬", 180, 88);
console.log(jang);
console.log(park);
document.write(jang.getInfo());
document.write(jang.getResult());
</script>
</head>
<body>
</body>
</html>
*λ©λͺ¨λ¦¬ μ μ½μ μν νλ‘ν νμ μ¬μ©νκΈ°
ν¨μλ₯Ό μ¬λ¬ κ° λ±λ‘νλ©΄ λ©λͺ¨λ¦¬ 곡κ°μ λ§μ΄ μ°¨μ§νμ¬ λ©λͺ¨λ¦¬λ₯Ό λλΉνκ² λλ€. μ΄λ΄ λλ κ°μ²΄ μμ±μ ν¨μμ νλ‘ν νμ μ μ¬μ©νμ¬ ν¨μλ₯Ό λ±λ‘νλ©΄ λ©λͺ¨λ¦¬ λλΉλ₯Ό μ€μΌ μ μλ€.
β
μμ )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset = "UTF-8">
<title> ν¨μ </title>
<script>
function CheckWeight(name, height, weight) {
this.userName = name;
this.userHeight = height;
this.userWeight = weight;
this.minWeight;
this.maxWeight;
}
CheckWeight.prototype.getInfo = function() {
var str = ""
str += "μ΄λ¦: " + this.userName + ", ";
str += "ν€: " + this.userHeight + ", ";
str += "λͺΈλ¬΄κ²: " + this.userWeight + "<br>";
return str;
}
CheckWeight.prototype.getResult = function( ) {
this.minWeight = (this.userHeight - 100) * 0.9 - 5;
this.maxWeight = (this.userHeight - 100) * 0.9 + 5;
if(this.userWeight >= this.minWeight &&
this.userWeight <= this.maxWeight) {
return "μ μ λͺΈλ¬΄κ²μ
λλ€";
} else if(this.userWeight < this.minWeight) {
return "μ μ λͺΈλ¬΄κ²λ³΄λ€ λ―Έλ¬μ
λλ€";
} else {
return "μ μ λͺΈλ¬΄κ²λ³΄λ€ μ΄κ³Όμ
λλ€";
}
}
var jang = new CheckWeight("μ₯보리", 168, 62);
var park = new CheckWeight("λ°λ¬μ¬", 180, 88);
console.log(jang);
console.log(park);
document.write(jang.getInfo());
document.write(jang.getResult(), "<br>");
document.write(jang.getResult === park.getResult);
</script>
</head>
<body>
</body>
</html>
βΆ μλ°μ€ν¬λ¦½νΈ λ΄μ₯ ν¨μ
λ΄μ₯ ν¨μλ μλ°μ€ν¬λ¦½νΈ μμ§μ λ΄μ₯λ ν¨μλ₯Ό λ§νλ€.
μ’ λ₯ |
μ€λͺ |
μ¬μ© μ |
encodeURI() |
λ¬Έμλ₯Ό μ λ μ½λκ°μΌλ‘ μΈμ½λ©νλ€. (μλ¬Έ, μ«μ, μΌλΆ κΈ°νΈ(;,/?:@&=+$)λ μ μΈ) |
encodeURI("?query=κ°"); →"?query=%EA%B0%91" |
encodeURIComponent() |
λ¬Έμλ₯Ό μ λ μ½λκ°μΌλ‘ μΈμ½λ©νλ€. (μλ¬Έ, μ«μ μ μΈ) |
encodeURIComponent("?query=κ°"); → "%3Fquery%3D%EA%B0%91" |
decodeURI() |
μ λ μ½λκ°μ λμ½λ©ν΄ λ€μ λ¬Έμννλ€. |
encodeURIComponent("?query=κ°") → "%3Fquery%3D%EA%B0%91" |
decodeURIComponent() |
μ λ μ½λκ°μ λμ½λ©ν΄ λ€μ λ¬Έμννλ€. |
decodeURIComponent("%3Fquery%3D%EA%B0%91") → "?query=κ°" |
parseInt() |
λ¬Έμμ΄ λ°μ΄ν°λ₯Ό μ μν λ°μ΄ν°λ‘ λ°ννλ€. |
parseInt("5.12") → 5 parseInt("15px") → 15 |
parseFloat() |
λ¬Έμμ΄ λ°μ΄ν°λ₯Ό μ€μν λ°μ΄ν°λ‘ λ°ννλ€. |
parseInt("5.12") → 5.12 parseInt("65.5%") → 65.5 |
String() |
λ¬Έμν λ°μ΄ν°λ‘ λ°ννλ€. |
String(5) → "5" |
Number() |
μ«μν λ°μ΄ν°λ‘ λ°ννλ€. |
Number("5") → 5 |
Boolean() |
λ Όλ¦¬ν λ°μ΄ν°λ‘ λ°ννλ€. |
Boolean(5) → true Boolean(null) → false |
inNaN() |
is Not a Numberμ μ½μμ΄λ©° μ«μκ° μλ λ¬Έμκ° ν¬ν¨λμ΄ μμΌλ©΄ trueλ₯Ό λ°ννλ€. |
isNaN("5-3") → true isNaN("53") → false |
eval() |
λ¬Έμν λ°μ΄ν°λ₯Ό λ°μ΄νκ° μλ μλ°μ€ν¬λ¦½νΈ μ½λλ‘ μ²λ¦¬νλ€. |
eval("15+5") → 20 |
'JavaScript > JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λλ¦Όμ½λ© by μ리] JavaScript κΈ°μ΄ κ°μ(2) (ES5+) (0) | 2021.10.06 |
---|---|
[λλ¦Όμ½λ© by μ리] JavaScript κΈ°μ΄ κ°μ(1) (ES5+) (0) | 2021.10.05 |
[JavaScript] κ°μ²΄ (0) | 2021.02.11 |
[JavaScript] λ°λ³΅λ¬Έ (0) | 2021.02.02 |
[JavaScript] μ μ΄λ¬Έ (0) | 2021.02.02 |
λκΈ