-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_in_depth_9
Init-time branching ํจํด์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๋งค์ฐ ์ ์ฉํ ํจํด ์ค ํ๋์ด๋ค. ์ด๋ฆ ๊ทธ๋๋ก ์ด๊ธฐํ ๋จ๊ณ์์ ๋ถ๊ธฐํ์ฌ ๊ฐ์ ํจ์๋ฅผ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ ์ํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ๋ณดํต ์นํ์ด์ง๊ฐ ์ฒ์ ์ด๋ฆด ๋ ์คํ๋๋ค๊ณ ํด์ Init-branching ํจํด์ด๋ผ๊ณ ๋ ํ๋ฉฐ, ํ์ด์ง๊ฐ ๋ก๋๋ ๋ ์คํ๋๊ธฐ ๋๋ฌธ์ Load-branching ํจํด์ด๋ผ๊ณ ๋ ํ๋ค.
์ด ํจํด์ด ์ ์ฉํ ์ด์ ๋ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋ค์ํ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ ์ ๊ณต๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ ์ฃผ์ ๊ธฐ๋ฅ์ ๋ํด์ ์ด๋ฌํ ํธํ์ฑ์ ๋ณด์ฅํด์ค ๋ ์ฌ์ฉํ๋ฉด ์ข์ ํจํด์ด๋ค.
Init-time branching ํจํด์ ์ฅ์ ์ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์ด๋ ์ํ์ ๋ฐ๋ผ์ ๊ฐ์ ๊ธฐ๋ฅ์ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ณตํ ์ ์์ด์ ๋งค์ฐ ์ ์ฉํ๋ค. ํ์ง๋ง ์ด์ฐ๋์๋ ์ต์ด ์ด๊ธฐํ ์์ ์ ๊ทธ๋ฌํ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ๊ณผ ์ํ๋ฅผ ํ์ธํ๊ธฐ ์ํ ์ถ๊ฐ ์ปดํจํ ์์์ด ์๋ชจ๋๋ฏ๋ก ์ต์ด ์ด๊ธฐํ ๋ ์๋ชจํ๋ ๊ฒ์ด ๋์์ง, ์๋๋ฉด ๋งค๋ฒ ์ปดํจํ ์์์ ์๋ชจํ๋ ๊ฒ์ด ๋์์ง ํ๋จํ์ฌ ์ ํํ๋ฉด ์ข๋ค.
// ํ์ค ํธํ์ ์ํ ์ด๋ฒคํธ ํธ๋ค๋ฌ ํจ์ ์ ์ ๋ฐฉ๋ฒ
// ๋ธ๋ผ์ฐ์ ์ข
๋ฅ์ ๋ฒ์ ์ ๋ฐ๋ผ์ ๊ตฌ๋ถํ๋ ๊ฒ์ด ์๋๋ผ
// ํ์ฌ ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ฅ ์ฌ๋ถ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๊ณตํ๋ ๋ฐฉ๋ฒ์ด ์ข๋ค.
(function () {
if (!Element.prototype.addEventListener) {
if (Element.prototype.attachEvent) {
Element.prototype.addEventListener = function (type, fn) {
this.attachEvent("on" + type, fn);
}
} else {
Element.prototype.addEventListener = function (type, fn) {
this["on" + type] = fn;
}
}
}
if (!Element.prototype.removeEventListener) {
if (Element.prototype.detachEvent) {
Element.prototype.removeEventListener = function (type, fn) {
this.detachEvent("on" + type, fn);
}
} else {
Element.prototype.removeEventListener = function (type) {
this["on" + type] = null;
}
}
}
}());XMLHttpRequest ๊ฐ์ฒด๋ ํ์ฌ ๋๋ถ๋ถ ๋ธ๋ผ์ฐ์ ์์๋ ์ง์ํ๋, Internet Explorer ๋ฎ์ ๋ฒ์ ์์๋ ์ฌ์ฉํ ์ ์์ด์ ์ด๋ฅผ ์ํ ํธํ์ฑ ์ค์ ์ด ํ์ํ๋ค.
// Init-time branching์ ํตํ getXHR() ํจ์ ์ ์
var getXHR = (function () {
var xhr;
if (window.XMLHttpRequest) {
return function () {
return new XMLHttpRequest();
}
}
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP.6.0");
return function () {
return new new ActiveXObject("MSXML2.XMLHTTP.6.0");
}
} catch (e) {
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP.3.0");
return function () {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
} catch (e) {
alert("This browser does not support XMLHttpRequest")
}
}
}());