scope of vue js code example
Example 1: scope of this keyword in vue js
<script>
export default {
data:() => ({
myData: 1
}),
mounted() {
this.myMethod()
},
methods:{
myMethod() {
window.addEventListener('click', () => {
console.log('this >', this.myData)
})
}
}
}
</script>
Example 2: scope of this keyword in vue js
<script>
export default {
mounted() {
this.myMethod()
},
methods:{
myMethod() {
let _this = this
let self = this
let that = this
window.addEventListener('click', function() {
console.log('this scope (function) >', this)
console.log('this scope (vuejs) >', _this)
console.log('this scope (vuejs) >', self)
console.log('this scope (vuejs) >', that)
})
}
}
}
</script>