Skip to content

Instantly share code, notes, and snippets.

View rrameshbtech's full-sized avatar

Ramesh Ramalingam rrameshbtech

  • ThoughtWorks
  • Coimbatore, India
  • 18:32 (UTC +05:30)
View GitHub Profile
@rrameshbtech
rrameshbtech / default-bindings-gobal.js
Last active July 25, 2018 16:13
JavaScript-Magic Words:this
var city = 'Karur';
console.log(this.city); // Karur
console.log(city); //Karur
@rrameshbtech
rrameshbtech / default-binding-this-nodejs.js
Last active July 30, 2018 09:16
How this works in nodes global
(function (exports, require, module, __filename, __dirname) {
//****Your code starts here****//
var city = 'Karur';
console.log(this.city); // undefined
console.log(city); //Karur
//****Your code ends here****//
});
var args = [self.exports, require, self, filename, dirname];
return compiledWrapper.apply(self.exports, args);
var slang = 'Kongu';
function printSlang() {
console.log(this.slang); // Kongu
}
printSlang();
// Run it in Browser
function thisInBrowser() {
console.log(this === window); // true
}
thisInBrowser();
//Run it in Node.js
function thisInNodeJS() {
console.log(this === global); // true
}
let manchesterOfSouthIndia = {
city: 'Coimbatore',
speciality: 'cotton industries',
getSpeciality: function() {
return this.city + ' is known for ' + this.speciality;
}
}
console.log(manchesterOfSouthIndia.getSpeciality()); // Coimbatore is known for cotton industries
let manchesterOfSouthIndia = {
city: 'Coimbatore',
speciality: 'mechanical industries',
}
manchesterOfSouthIndia.getSpeciality = function() {
return this.city + ' is known for ' + this.speciality;
}
console.log(manchesterOfSouthIndia.getSpeciality()); // Coimbatore is known for mechanical industries
@rrameshbtech
rrameshbtech / explicit-binding-this.js
Last active July 30, 2018 09:21
Explicit binding of this using call & apply
const siliconVallyOfIndia = {
city: 'Bengaluru',
speciality: 'software industries'
};
const texCity = {
city: 'Tiruppur',
speciality: 'textile industries'
};
function printSpeciality() {
console.log(`${this.city} is know for ${this.speciality}`);
}
const printKarurSpeciality = printSpeciality.bind({city: 'Karur', speciality: 'Textile Industries'});
printKarurSpeciality(); // Karur is know for Textile Industries
const printCoimbatoreSpeciality = printSpeciality.bind({city: 'Coimbatore', speciality: 'Mechanical Industries'});
printCoimbatoreSpeciality(); // Coimbatore is know for Mechanical Industries
<html>
<button id="thisButton">Check this</button>
<script>
const thisButton = document.getElementById('thisButton');
thisButton.addEventListener('click', verifyThis);
function verifyThis(event) {
//this is bound to the button onwhich the click event is triggered
console.log(this === thisButton); // true
console.log(this === event.currentTarget); // true
<html>
<button id="thisButton" onclick="verifyThis(this)">Check this</button>
<script>
const thisButton = document.getElementById('thisButton');
function verifyThis(buttonWhichTriggered) {
console.log(buttonWhichTriggered === thisButton); // true
console.log(this === window); // true
}
</script>