javascript 의 this 분석
this 는 서로 다른 상하 문 환경 에서 서로 다른 대상 을 가리 키 며, 성명 환경 과 무관 하 며, 운행 환경 과 관계 가 있다
var name = 'jack'
function sayName () {
console.log(this.name)
}
sayName() // jack
// ,
function sayName () {
console.log(this.name)
}
var o = {}
o.name = 'jack'
o.sayName = sayName
o.sayName() // jack
// ,this
1.
function Test () {
this.x = 1
}
var o = new Test()
o.x //1
2. this ,
var x = 0
function Test () {
this.x = 1
}
var o = new Test()
o.x // 1
3.
function Person(name){
this.name = name
}
person.prototype.sayName = function () {
console.log(this.name)
}
var xiaoming = new Person('xiaoming')
xioaming.sayName() // xiaoming
var name = 'rose'
function sayName () {
console.log(this.name)
}
var o = {}
o.name = 'jack'
o.sayName = sayName
o.sayName.apply() // rose ,
o.sayName.apply(o) // jack this o
이 분석 이 어렵다.
// this
var name = "Bob";
var nameObj ={
name : "Tom",
showName : function(){
alert(this.name);
},
waitShowName : function(){
setTimeout(this.showName, 1000);
}
};
nameObj.waitShowName(); // Bob
이상 의 문 제 를 해결 하기 위해 서, 우 리 는 먼저 아래 this 의 방향 을 한 걸음 한 걸음 볼 수 있다.
var person = {
name: 'jack',
sayName: function () {
console.log(this.name)
}
}
var anotherPerson = {
name: 'rose',
sayName: person.sayName
}
anotherPerson.sayName() // rose
// this person.sayName , anotherPerson.sayName, this anotherPerson, rose
var name = 'jack'
var o = {
name: 'rose',
sayName: function () {
console.log(this.name)
}
}
var sayName = o.sayName
sayName() // jack
// ,sayName() , this window,window.name === 'jack'
var name = 'willie'
var Jack = {
name: 'jack',
sayName: function () {
console.log(this.name)
}
}
var Rose = {
name: 'rose',
sayName: function () {
var fun = Jack.sayName
fun()
}
}
Rose.sayName() // willie
// , , this Rose, window
브 라 우 저 에서 setTimeout, setInterval, 익명 함수 가 실 행 될 때의 현재 대상 은 전역 대상 window 입 니 다.
var name = 'wille'
var o = {
name: 'jack',
sayName: function () {
setTimeout(function () {console.log(this.name)}, 100)
}
}
o.sayName() // wille
// ES6 ,
var name = 'wille'
var o = {
name: 'jack',
sayName: function () {
setTimeout(() => {console.log(this.na)},100)
}
}
o.sayName() //jack
// setTimeout,setInterval, , ,this , 。
사고 문제, 아래 의 this 가 무엇 을 가리 키 는 지, 그리고 원인 을 생각 하 라.
1:
var obj = {
say: function () {
setTimeout(() => {
console.log(this)
}, 0)
}
}
obj.say() //obj
2:
function test() {
console.log(this)
}
test() // window( )
3:
var obj = {
say: function () {
function _say () {
console.log(this)
}
return _say.bind(obj)
}() // , obj , this window
}
obj.say() // window( )
4:
var obj = {}
obj.say = function () {
function _say () {
console.log(this)
}
return _say.bind(obj)
}() // , obj , this obj
obj.say() // obj
5:
var obj = {
sayName: function () {
console.log('outer', this)
return function () {
console.log('inner', this)
}
}()
}
// ,this window,
// , this window
obj.sayName() //outer -> window inner -> obj
// , obj.sayName() this obj
// obj
var obj = {
sayName : function () {
console.log('inner', this)
}
}
obj.sayName() // obj
// , obj sayName ,this obj
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.