본문 바로가기
728x90
300x250
SMALL

javaScript545

이벤트 리스너 안에서 this var value=10; $(document).ready(function(){ // 이벤트 리스너 등록 $("#btn").click(function(){ this.value=20; value=30; console.log("1. value = "+value); console.log("2. this.value = "+this.value); console.log("3. window.value = "+window.value); }); }); /* 실행결과 1. value = 30 2. this.value = 20 3. window.value = 30 */ 이벤트 리스너에서의 this는 이벤트를 발생시킨 객체를 가르킨다. 그렇기 때문에 this.value=20; 여기서의 this는 #btn이 된다. 그렇기때문에 .. 2023. 8. 11.
중첩 함수 안에서 this var value = 10; function outer() { function inner(){ this.value = 20; value = 30; document.write("1. value = " + value, " "); document.write("2. this.value = " + this.value, " "); document.write("3. window.value = " + window.value, " "); } inner(); } outer(); /* 실행결과 1. value = 30 2. this.value = 30 3. window.value = 30 */ 일반 중첩 함수에서도 this는 window가 된다. 즉 this.window와 동일하기 때문에 value에 20이 저장된다. va.. 2023. 8. 9.
javaScript call() 메서드 call() javaScript 함수의 내장 메서드 중 하나로, 다른 객체의 컨텍스트에서 함수를 호출하는 기능을 제공한다. 즉 call()메서드를 사용하면 함수를 호출하면서 함수 내부에서 사용되는 this값을 명시적으로 지정할 수 있다. 문법 functionName.call(thisArg, arg1, arg2, ...); functionName : 호출하려는 함수의 이름 thisArg : 함수 내에서 this로 사용될 객체를 지정 arg1, arg2, ... : 호출하려는 함수에 전달할 인수들을 나열한다. var person = { name: "John", age: 30, greet: function() { console.log("Hello, my name is " + this.name + " and I.. 2023. 8. 8.
일반 함수 안에서 this var value = 10; function execute() { this.value = 20; value = 30; document.write("1. value = " + value, " "); document.write("2. this.value = " + this.value, " "); document.write("3. window.value = " + window.value, " "); } execute(); 실행결과 1. value = 30 2. this.value = 30 3. window.value = 30 일반함수 내부에 있는 this는 전역객체인 window가 저장된다. 즉 this.value는 window.value와 같은 의미이다. 즉 전역 객체인 value에 20이 저장되게 된다. .. 2023. 8. 6.
728x90
반응형
LIST