ํจ์
๋ค๋ฅธ ์ธ์ด์ ํ์ฉ์ด ์ ์ฌํด์ ์ค์ต๋ง ํด๋ณด๊ณ ๋์ด๊ฐ๋๋ก ํ๊ฒ ๋ค.
<!DOCTYPE html>
<html>
<h1>Function</h1>
<h2>Basic</h2>
<ul>
<script>
function two(){
document.write('<li>2-1</li>');
document.write('<li>2-2</li>');
}
document.write('<li>1</li>');
two();
document.write('<li>3</li>');
two();
</script>
</ul>
<h2>Parameter & Argument</h2>
<script>
function onePlusOne(){
document.write(1+1+'<br>');
}
onePlusOne();
function sum(left, right){
document.write(left+right+'<br>');
}
sum(2,3); // 5
sum(3,4); // 7
</script>
<h2>Return</h2>
</body>
</html>
ํจ์ ์ ์ ๋ฐฉ๋ฒ
function(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2 ......){
ํจ์ ๋ด์ฉ;
}
ํจ์์ ๋ฆฌํดํ์ ๋ํด ์ดํด๋ณด์.
<!DOCTYPE html>
<html>
<h1>Function</h1>
<h2>Basic</h2>
<ul>
<script>
function two(){
document.write('<li>2-1</li>');
document.write('<li>2-2</li>');
}
document.write('<li>1</li>');
two();
document.write('<li>3</li>');
two();
</script>
</ul>
<h2>Parameter & Argument</h2>
<script>
function onePlusOne(){
document.write(1+1+'<br>');
}
onePlusOne();
function sum(left, right){
document.write(left+right+'<br>');
}
function sumColorRed(left, right){
// document.write('<div style="color:red">'+(left+right)+'</div><br>');
document.write('<div style="color:red">'+left+right+'</div><br>');
}
sum(2,3); // 5
sumColorRed(2,3); // 23 //์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ฌธ์์ ์ซ์๋ฅผ ๋ํ๋ฉด ์ซ์๋ฅผ ๋ฌธ์๋ก ๊ฐ์ฃผํ๊ธฐ ๋๋ฌธ์
//+๋ฅผ ๋ง์
์ด ์๋ ๋ฌธ์๊ฒฐํฉ์ฐ์ฐ์๋ก ์ฌ์ฉํ๋ค. ๋ฐ๋ผ์ 23์ด ๋์จ๋ค.
sum(3,4); // 7
</script>
<h2>Return</h2>
<script>
function sum2(left, right){
return left+right;
}
document.write(sum2(2,3)+'<br>');
document.write('<div style="color:red">'+sum2(2,3)+'</div>');
document.write('<div style="font-size:3rem;">'+sum2(2,3)+'</div>');
</script>
</body>
</html>
์ด ์ฝ๋์์๋ ์ฃผ์์ฒ๋ฆฌํ ๋ถ๋ถ๋ง ๋์ฌ๊ฒจ ๋ณด๋ฉด ๋ ๋ฏ ํ๋ค. ์๋ฐ ์คํฌ๋ฆฝํธ๋ ๋ฌธ์์ ์ซ์๋ฅผ ๋ํ๋ฉด ์ซ์๋ฅผ ์ซ์๋ฅผ ๋ฌธ์๋ก ๊ฐ์ฃผํ๊ธฐ ๋๋ฌธ์ +๋ฅผ ๋ง์ ์ด ์๋ ๋ฌธ์๊ฒฐํฉ์ฐ์ฐ์๋ก ์ฌ์ฉํ๋ค.
๊ฐ์ฒด
์ด๋ฒ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ , ๋ ๊ทธ ๋ฐ์ดํฐ์ ์ ๊ทผํด ๋ณผ ๊ฒ์ด๋ค.
๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ + ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฉ๋ฒ
var coworkers={
"programmer":"egoing",
"designer":"leezche"
};
๋ฐ์ดํฐ๋ ์ด๋ฐ์์ผ๋ก ์ถ๊ฐํ ์ ๋ ์๋ค.
coworkers.bookkeeper="duru"; //coworkers๋ ๊ฐ์ฒด ์ด๋ฆ
coworkers["data scientist"]="taeho"; //๋์ด์ฐ๊ธฐ๊ฐ ์์ ๋
๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ
document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
document.write("data scientist : "+coworkers["data scientist"]+"<br>");
์ฆ ์ ์ฒด ์ฝ๋์ ์คํ๋ด์ฉ์ ์๋์ ๊ฐ์ด ๋๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Object</h1>
<h2>Create</h2>
<script>
var coworkers={
"programmer":"egoing",
"designer":"leezche"
};
document.write("programmer : "+coworkers.programmer+"<br>");
document.write("designer : "+coworkers.designer+"<br>");
coworkers.bookkeeper="duru";
coworkers["data scientist"]="taeho";
document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
document.write("data scientist : "+coworkers["data scientist"]+"<br>");
</script>
</body>
</html>
์ด์ ๊ฐ์ฒด์ ๋ฐ๋ณต๋ฌธ์ ํจ๊ป ์ฌ์ฉํด๋ณด์. ์ฌ์ฉ๋ฒ์ ํ์ด์ฌ๊ณผ ์๋นํ ์ ์ฌํ๋ค. ์ฐ์ ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ ๋ถ๋ถ์ ๋ณด๋ฉด
key : value ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ธํ๋ค.
var ๋ณ์์ด๋ฆ{
key1 : value1
key2 : value2
}
์ด๋ฌํ key value๋ฅผ ์ด์ฉํ์ฌ ๋ฐ๋ณต๋ฌธ์ ์์ฑํด๋ณด์.
for(var key in coworkers){
document.write(key+"<br>");
}
์ด๋ ๊ฒ ํ๋ฉด key๊ฐ๋ง์ ๋ถ๋ฌ์ฌ ์ ์๋ค.
์ด์ ์ด๊ฒ์ ์ด์ฉํด ์์ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํด๋ณด๋ฉด ์๋์ ๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Object</h1>
<h2>Create</h2>
<script>
var coworkers={
"programmer":"egoing",
"designer":"leezche"
};
document.write("programmer : "+coworkers.programmer+"<br>");
document.write("designer : "+coworkers.designer+"<br>");
coworkers.bookkeeper="duru";
coworkers["data scientist"]="taeho";
document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
document.write("data scientist : "+coworkers["data scientist"]+"<br>");
</script>
<h1>Iterate</h1>
<script>
for(var key in coworkers){
document.write(key+" : "+coworkers[key]+"<br>");
}
</script>
</body>
</html>
์คํ ํ๋ฉด๋ ์๋ ์์ฑํ๋ ์ฝ๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์์ ์ ์ ์๋ค.
์ด์ ์ด๊ฒ์ ํจ์๋ก ๋ง๋ค์ด์ฌ์ฉํ๋ฉด ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Object</h1>
<h2>Create</h2>
<script>
var coworkers={
"programmer":"egoing",
"designer":"leezche"
};
document.write("programmer : "+coworkers.programmer+"<br>");
document.write("designer : "+coworkers.designer+"<br>");
coworkers.bookkeeper="duru";
coworkers["data scientist"]="taeho";
document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
document.write("data scientist : "+coworkers["data scientist"]+"<br>");
</script>
<h1>Iterate</h1>
<script>
for(var key in coworkers){
document.write(key+" : "+coworkers[key]+"<br>");
}
</script>
<h2>Property & Method</h2>
<script>
coworkers.showAll = function(){
for(var key in this){
document.write(key+' : '+this[key]+'<br>');
}
}
coworkers.showAll();
</script>
</body>
</html>
์ด ์ฝ๋์์ this๋ ํจ์๊ฐ ํฌํจ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ๋ค๋ง ์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด
์ด๋ ๊ฒ ๋งจ ๋ฐ์ค ๋ถ๋ถ๋ ํ๋ฉด์ ๋์ค๋๋ฐ ์ด๊ฒ์ if๋ฌธ์ ์ด์ฉํด์ ์ง์ธ ์ ์๋ค๊ณ ํ๋ค.
๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ์ด์ฉํด ์์ ๋ง๋ค์๋ ์ฝ๋๋ฅผ ์ ๋ฆฌํ๋ฉด ์๋์ ๊ฐ๋ค.
<!doctype html>
<html>
<head>
<title>WEB1 - JavaScript</title>
<meta charset="utf-8">
<body>
<script>
function LinksSetColor(color){
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = color;
i = i + 1;
}
}
var Links = {
setColor:function(color){
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = color;
i = i + 1;
}
}
}
function BodySetColor(color){
document.querySelector('body').style.color = color;
}
function BodySetBackgroundColor(color){
document.querySelector('body').style.backgroundColor = color;
}
var Body = { //๊ฐ์ฒด๋ฅผ ํ์ฉํ ๋ถ๋ถ
setColor:function (color){
document.querySelector('body').style.color = color;
},
setBackgroundColor:function (color){
document.querySelector('body').style.backgroundColor = color;
}
}
function nightDayHandler(self){
var target = document.querySelector('body');
}
</script>
<h2>JavaScript</h2>
<p>
JavaScript (/หdสษหvษหskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
</p>
</body>
</html>
๋ ์ด๊ณณ์์ script๋ถ๋ถ๋ง์ ๋ณต์ฌํด์ ํ์ผ๋ช .jsํ์ผ๋ก ์ ์ฅํ ๋ค
<script src="ํ์ผ๋ช
.js"></script>
์ด๋ ๊ฒ ํ๋ฉด ์์ <script></script>๋ก ๋ฌถ์ธ ๋ถ๋ถ์ ๋์ฒดํ ์ ์๋ค. ์ด๋ ๊ฒ ํ๋ฉด ํ์ผ์ ์ ์ง๋ณด์์ ๋ณด๋ค ํธํด์ง๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํ๋ ์์ํฌ
https://webclub.tistory.com/458
UI vs API
https://velog.io/@rimu/UI-API-%EC%97%90-%EB%8C%80%ED%95%9C-%EC%A0%95%EB%A6%AC
ํด๋น ๋ถ๋ถ๋ค์ ์ ๊ธ๋ค์ ์์ธํ ์ค๋ช ์ด ๋์์๋ค.
'WEB > HTML+CSS+JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋๋ผ ์คํฌ๋ฆฝํธ - part 3์ ๋ฆฌ (0) | 2021.01.04 |
---|---|
๋ฐ๋๋ผ ์คํฌ๋ฆฝํธ - part 2 ์ ๋ฆฌ (0) | 2021.01.04 |
JavaScript ๋ด์ฉ์ ๋ฆฌ - 3 (0) | 2021.01.03 |
JavaScript ๋ด์ฉ์ ๋ฆฌ - 2 (0) | 2021.01.02 |
JavaScript ๋ด์ฉ์ ๋ฆฌ - 1 (0) | 2021.01.01 |
๋๊ธ