13. This
This
함수 호출 패턴
var outter = function(){
console.log('outter', this)
var inner = function(){
console.log('inner', this);
};
inner();
};
outter();메소드 호출 패턴
생성자 호출 패턴
Last updated
var outter = function(){
console.log('outter', this)
var inner = function(){
console.log('inner', this);
};
inner();
};
outter();Last updated
'use strict' // Strict 모드를 사용하겠다고 선언했습니다.
function func(){
console.log(this); // undefined
}
func();var person = {
name : 'olaf',
getName : function(){
console.log(this); // person
console.log(this.name); // olaf
}
};
person.getName();var person = {
name : 'olaf',
getName : function(){
console.log(this);
console.log(this.name);
function inner(){
console.log(this); // window
}
inner(); // 메서드 내부함수 실행
}
};
person.getName();var Person = function(name){
this.name = name; // this는 자신 즉, Person을 가리킨다.
};
Person.prototype.getName = function(){
console.log(this.name); // Person.prototype 이기때문에 프로토타입 내에서도 this는 자기자신 Person을 가리킵니다.
};var Person = function(name){
this.name = name; // this는 자신 즉, Person을 가리킨다.
};
Person.prototype.getName = function(){
console.log(this.name); // Person.prototype 이기때문에 프로토타입 내에서도 this는 자기자신 Person을 가리킵니다.
function inner(){
console.log(this); // window가 찍힙니다! 이제 눈에 보이시죠?!
}
inner();
};
var p = new Person('olaf');
p.getName();