일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- youtubethumbnail
- 이미지텍스트추출
- 아이옷
- 하단스크롤
- sublimetextgit연동
- 애니메니션효과
- 스크롤하단
- 연동하는방법
- 서브라임git연동하는 방법
- css애니메이션효과
- 아이프레임유튜브썸네일변경
- youtubeiframethumbnail
- 이미지텍스트변환
- 텍스트추출
- 해시스크롤이동
- 서브라임git
- css3 animate
- 하단fixed
- scroll이동
- 앵커이동
- PC에서텍스트추출
- 달콤한바느질
- css효과
- 하단특정위치
- sublime+git
- 서브라임+git
- 특정위치고정
- 아기옷
- css3예제
- 아이프레임유튜브썸네일
Archives
- Today
- Total
개인공간
자바스크립트 팁 #1, This 제대로 알고가자 (this와 window 객체, new ) 본문
반응형
https://www.zerocho.com/category/JavaScript/post/5b0645cc7e3e36001bf676eb
https://www.zerocho.com/category/JavaScript/post/5b0645cc7e3e36001bf676eb
www.zerocho.com
this가 window인지 obj인지 소스 코드
this; // Window {}
function a() { console.log(this); }; a(); // Window {}
var obj = {
a: function() { console.log(this); },
};
obj.a(); // obj
var a2 = obj.a; a2(); // window
a2는 obj.a를 꺼내온 것이기 때문에 더 이상 obj의 메서드가 아닙니다.
var obj2 = { c: 'd' };
function b() {
console.log(this);
}
b(); // Window
b.bind(obj2).call(); // obj2
b.call(obj2); // obj2
b.apply(obj2); // obj2
명시적으로 this를 바꾸는 함수 메서드 삼총사 bind, call, apply를 사용하면 this가 객체를 가리킵니다.
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHi = function() {
console.log(this.name, this.age);
}
Person('ZeroCho', 25);
console.log(window.name, window.age); // ZeroCho 25 -> //window
//obj로 실행되려면 new Person을 사용해야 합니다.
var hero = new Person('Hero', 33); // Person {name: "Hero", age: 33}
hero.sayHi(); // Hero 33
$('div').on('click', function() {
console.log(this); // <div>
function inner() {
console.log('inner', this); // inner Window
}
inner();
});
//inner() window를 가져옴
//obj를 불러오게 하려면.. 2가지 방법 that과 ES6 함수 사용
$('div').on('click', function() {
console.log(this); // <div>
var that = this;
function inner() {
console.log('inner', that); // inner <div>
}
inner();
});
//ES6 함수 사용
$('div').on('click', function() {
console.log(this); // <div>
const inner = () => {
console.log('inner', this); // inner <div>
}
inner();
});
반응형
'IT정보 > jQuery_javaScript' 카테고리의 다른 글
[제이쿼리] fixed였던 오브젝트 스크롤 하단 특정위치에 고정시키기 (0) | 2023.07.12 |
---|---|
[제이쿼리] 기본 가로스크롤 선택요소 중앙정렬 & 스크롤에 따른 처음과 끝 버튼 처리 PC와 M버전별 (0) | 2023.01.17 |
[제이쿼리] 풀페이지 스크롤 플러그인 (0) | 2023.01.03 |
[제이쿼리] scroll 반정도왔을때 animation처리 하기 (0) | 2022.10.06 |
bx슬라이드 정리된 (0) | 2019.07.23 |