Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Last active October 10, 2024 05:51
Show Gist options
  • Save kirilkirkov/ffa17b29f4086f73163613f288503e82 to your computer and use it in GitHub Desktop.
Save kirilkirkov/ffa17b29f4086f73163613f288503e82 to your computer and use it in GitHub Desktop.
How this works in VueJS
// In Browser - Window Object
// In Node - {}
console.log(this)
// In Browser - Window Object
// In Node - Node Object
function globalScoped() {
console.log(this)
}
globalScoped()
// In Browser - Object { name: "kiro", tagLine: "js" }
// In Node - Object { name: "kiro", tagLine: "js" }
function testInstance(name, tagLine) {
this.name = name;
this.tagLine = tagLine;
}
new testInstance('kiro', 'js')
// Error. Should call super
class myClass extends myExantable {
constructor() {
this.name = 'Kiro';
this.tagLine = 'js';
}
}
new myClass();
// In Browser - The Button Element
<button onclick="alert(this)">
click
</button>
// In Browser - The Button
<button id="test-btn">
click
</button>
<script>
const btn = document.getElementById('test-btn');
function showTest() {
console.log(this);
}
btn.addEventListener('click', showTest);
</script>
// In Browser - 'What'?
// In Node - 'What'?
// this is (colin const)
function ask() {
return this.question
}
const colin = {
question: 'What?',
ask
}
console.log(colin.ask())
// In Browser - 'What'?
// In Node - 'What'?
// this is (colin const)
function ask() {
return this.question
}
const colin = {
question: 'What?'
}
console.log(ask.call(colin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment