하나의 기능(function함수)을 만든다고할 때
그 기능을 나중에 원하는대로 변형하여 쓰고 싶을 때 그 변형값을 function에 넣는게 매개변수이고
function 덧셈(매개변수1, 매개변수2){
1 + 매개변수1 + 매개변수2
}
-> 1 + 매개변수1 + 매개변수2 = 함수function의 값
덧셈(3, 5);
-> 9
그걸 활용해서 넣는게 인자(3과 5)인거 같습니다.
매개 : 둘사이의 관계를 맺어줌
변수 : 변하는 수
매개변수 : 둘사이의 관계를 맺어주면서 변하는 수
function에서 결과값에 영향을 주는 변하는 수를 매개변수이고 그걸 function 함수이름(여기 괄호에 넣음)
매개변수를 내가 특정한 숫자나 데이터를 넣으면 그게 인자가 되는거 같아요.
그냥 x + y = ? 라는 함수에 x,y는 매개변수이고
내가 x에 1을 넣고 y에 2를 넣으면 1,2는 인자(구성요소)라고 하는것이고
매개변수에 내가 어떤 인자를 넣냐에 따라서 값이 달라지는 식이 함수라고 보면 될거같아요.
저도 구글링 해보다가 알았는데, target을 전역변수로 빼게 되면 웹페이지가 body태그가 생성되기 전에 target을 읽어버려서 실행이 안된다고 합니다.
반면에 target이 nightDayHandler함수 안에 들어가있게 되면 input태그까지 읽고(그럼 당연히 body태그는 읽혔겠죠?) nightDayHandler함수를 호출해서 target변수를 읽게 되기 때문에 실행이 되는 거라고 합니다!
혹시 몰라서 제가 참고한 사이트 링크 첨부할게요!!
저는 value의 값이 night일 때와 아닐때를 함수를 따로 nightHandler(self)와 dayHandler로 만들어 봤는데 실행이 안되는데 이유를 아시는 분이 계실까요 ㅜㅜ
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script>
var target = document.querySelector('body');
function nightHandler(self) {
target.style.backgroundColor = 'black';
target.style.color = 'white';
self.value = 'day'
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = 'powderblue';
i = i + 1;
}
function dayHandler(self) {
target.style.backgroundColor = 'white';
target.style.color = 'black';
self.value = 'night';
var alist = document.querySelectorAll('a');
var i = 0;
while(i < alist.length){
alist[i].style.color = 'blue';
i = i + 1;
}
</script>
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<input id="night_day" type="button" value="night" onclick="
if(this.value === 'night') {
nightHandler(this);
} else {
dayHandler(this);
}
">
<ol>
<li><a href="1.html">HTML</a></li>
<li><a href="2.html">CSS</a></li>
<li><a href="3.html">JavaScript</a></li>
</ol>
<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>