-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_in_depth_10
Self-defining function์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ณ์์ ํจ์๋ฅผ ์ฝ๊ฒ ์ฌ์ ์ํ๊ณ ๋ฎ์ด์ธ ์ ์๋ ์ฅ์ ์์ ๋์จ ๋์์ธ ํจํด์ด๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ก ๊ฐ๋ฐํ๋ค ๋ณด๋ฉด ํน์ ํจ์๋ฅผ ํธ์ถํ๊ธฐ ์ ์ ์ด๊ธฐํ ์์
์ ์ํํด์ผ ํ๋ ๋๊ฐ ๋ง๋ค. ์ด๋ด ๋ ์ต์ด ์ฆ์ ํธ์ถ ํจ์๋ฅผ ์ด์ฉํด์ Init-time branching ํจํด ๋ฑ์ผ๋ก ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ๋ ์์ง๋ง, ์์ฃผ ํธ์ถ๋์ง ์๊ฑฐ๋ ์น ํ์ด์ง์์์ ํ ๋ฒ๋ ํธ์ถ๋์ง ์๋ ํจ์๋ ์ด๊ธฐํ ๋จ๊ณ๋ฅผ ๋ฌด์กฐ๊ฑด ์คํํ๋๋ก ํ๋ ๊ฒ์ ์์์ ์ถ๊ฐ๋ก ์๋ชจํ๊ฒ ๋๋ค.
์ด๋ด ๋ Self-defining function ํจํด์ ์ฌ์ฉํ๋ฉด ์ด๊ธฐํ ๋จ๊ณ๋ค์ ์ปดํจํ
์์ ์๋ชจ๋ฅผ ๋ถ์ฐํ ์ ์๋ค. ์ด๊ธฐํ ๋จ๊ณ๋ฅผ ๋ถ์ฐํจ์ผ๋ก์จ ์ป์ ์ ์๋ ์ด์ ์ ๋ฐ๋ก ์ฌ์ฉ์ ๊ฒฝํ์ด ๊ฐ์ ๋ ์ ์๋ค๋ ์ ์ด๋ค.
Self-defining function ํจํด์ ์ฅ์ ์ ์ธ๋ถ์์ ํด๋น ํจ์๋ฅผ ํธ์ถํ ๋ ๋ณด์ด๋ ๋ณ๊ฒฝ ์ ์ ์์ง๋ง, ๋ด๋ถ์ ์ผ๋ก ์ด๊ธฐํ ๋จ๊ณ๋ฅผ ๊ฑฐ์น๊ณ ๋์ ์ดํ์ ์ฒ๋ฆฌ ํ๋ก์ธ์ค๋ฅผ ์ต์ ํํ ์ ์๋ค๋ ์ ์ด๋ค. ๋์ ๋ฐ๋ผ์๋ ์๋์ผ๋ก ์์ฑ๋๋ ํด๋ก์ ๋ฅผ ํ์ฉํด์ ์ด๊ธฐํ ๋จ๊ณ์์ ์์ฑ๋๊ฑฐ๋ ์์งํ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๊ณ ์์ ์ ์์ผ๋ฏ๋ก ์ผํ์ฑ ์ฒ๋ฆฌ๋ฅผ ์์ฉํ๊ธฐ์ ์ข์ ํจํด์ด๋ค. ๋จ์ ์ด๋ผ๋ฉด ๋ถํ์ํ ํด๋ก์ ๊ฐ ์์ฑ๋๊ณ ๋ฐ์ดํฐ๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ๊ณ์ ๋จ์์์ ์ ์๋ค. ๋ฐ๋ผ์ ํด๋น ๋ณ์๋ฅผ ํ ๋ฒ๋ง ์ฌ์ฉํ๊ณ ์ฌ์ฉ ์ ํ ๊ฒ์ด๋ผ๋ฉด ๊ทธ๋ฅ undefined๋ก ์ด๊ธฐํ ํ๋ ๊ฒ์ด ์ข๋ค.
// Self-defining function ํจํด์ ์ด์ฉํ getXHR ์ ์
// getXHR()์ ์ต์ด๋ก ํธ์ถ๋ ๋ XMLHttpRequest๋ฅผ ์ ์ํ ์ง ๋ถ๊ธฐํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ
// ํธ์ถ๋ getXHR() ํจ์๋ฅผ ๋ค์ ์ ์ํ์ฌ ์ดํ์๋ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋๋ก ํ๋ค.
var getXHR = function () {
var xhr;
if (window.XMLHttpRequest) {
getXHR = function () {
return new XMLHttpRequest();
};
return new XMLHttpRequest();
}
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP.6.0");
getXHR = function () {
return new new ActiveXObject("MSXML2.XMLHTTP.6.0");
};
return xhr;
} catch (e) {
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP.3.0");
getXHR = function () {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
};
return xhr;
} catch (e) {
alert("This browser does not support XMLHttpRequest")
}
}
};// Self-defining function ํจํด์ ์ด์ฉํ ํจ์ ์ด๊ธฐํ
// ์ต์ด ํธ์ถ์ DOM ํ
ํ๋ฆฟ ์์ฑ๊ณผ DOM ํ์ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ์ด์ฉํ๊ณ
// ๋ณ๊ฒฝ์ด ํ์ํ ๋ถ๋ถ๋ง ์์ ํ๋ ๋ฐฉ์
<html>
<head>
<style>
#commentWrapper {
width: 200px;
}
.comment {
width: 150px;
display: inline-block;
}
.name {
width: 40px;
display: inline-block;
}
</style>
</head>
<body>
<div id="commentWrapper">
<div>
<div class="comment">Comment</div>
<div class="name">Name</div>
</div>
</div>
<form id="formComment">
<label>Comment: <input type="text" id="comment"></input></label>
<label>Name: <input type="text" id="name"></input></label>
<input type="submit"></input>
</form>
<script>
(function () {
var addComment = function () {
var divCommentWrapper = document.getElementById("commentWrapper"),
divCommentRow = document.createElement("div"),
divComment = document.createElement("div"),
divName = document.createElement("div"),
inputComment = document.getElementById("comment"),
inputName = document.getElementById("name");
divComment.className = "comment";
divName.className = "name";
divCommentRow.appendChild(divComment);
divCommentRow.appendChild(divName);
addComment = function () {
divComment.innerHTML = inputComment.value;
divName.innerHTML = inputName.value;
inputComment.value = "";
inputName.value = "";
divCommentWrapper.appendChild(divCommentRow.cloneNode(true));
};
addComment();
};
document.getElementById("formComment").addEventListener("submit", function () {
addComment();
event.returnValue = false;
return false;
});
}());
</script>
</body>
</html>// Self-defining function ํจํด์ ์ด์ฉํ ์ธ์ฆ ์ ๋ณด ๋ณด๊ด ๋ฐ ์ค๋ณต ์์ฒญ ๋ฐฉ์ง
(function () {
var requestAuthentication = function (information) {
var _requestAuthentication = requestAuthentication,
authInformation = null;
requestAuthentication = function (information) {
if (authInformation === null) {
alert("Already requesting");
return;
} else {
return authInformation;
}
}
sendRequest(information);
function sendRequest(information) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/auth");
xhr.onload = function () {
alert("Authorized!");
authInformation = xhr.responseText;
// ์ํ์์ ์๋ ์ธ์ฆ ์์ฒญ ํจ์๋ก ์ค์ ํด๋์ ์๋ ์๋ค.
// requestAuthentication = _requestAuthentication
};
xhr.onerror = function () {
if (confirm("Error occurred, send again?")) {
sendRequest(information);
}
}
xhr.send(information);
}
}
requestAuthentication("name=hello&password=world");
}());