var value = "Global Object"; function FirstValue(){ //이것은 변수 선언이다. 호출되기 전에는 존재하지 않는 것 var context1 = "This is the First Button"; console.log(context1); } function SecondValue(){ var context1 = "This is the Second Button"; } function ThirdValue(){ var context1 = "This is the Third Button"; } function alertValue(){ alert(value); alert(this.value); alert(this.context1); } FirstValue.alertValue = alertValue; SecondValue.alertValue = alertValue; ThirdValue.alertValue = alertValue; //내부 console.log로 context1 값이 출력되는거 확인 FirstValue(); //console.dir은 해당 객체의 프로퍼티를 로그하는 메소드이다. //함수 호출전에는 존재하지 않는 context1은 당연히 나오지 않는다. //따라서 내부 프로퍼티 형성을 해야만 출력될수 있다. FirstValue.propertyTest = "이런식으로 프로퍼티를 형성한다."; console.dir(FirstValue); var buttons = document.getElementsByClassName("test"); buttons[0].onclick = FirstValue.alertValue; buttons[1].onclick = SecondValue.alertValue; buttons[2].onclick = ThirdValue.alertValue;
<input class="test" type="button" value = "First"> <br/> <input class="test" type="button" value = "Two"> <br/> <input class="test" type="button" value = "Three"> <br/>
input { width: 70px; margin:20px 0px 0px 0px}