The Concept of `this` in JavaScript

The Concept of this in JavaScript!

TL;DR

Concept #1


let myName = new String('Ahmad Awais');
typeOf(myName); // object <=== this is the resulting object.

Concept #2

let person = {
name: "Ahmad Awais"
};
let sayMyName = function() {
console.log(this.name);
};
sayMyName.call(person); //<== this = obj 'person' that is passed in.

Concept #3

let person = {
name: "Ahmad Awais",
sayMyName: function() {
console.log(this.name);
}
};
person.sayMyName(); //<== this = obj 'person' on which the functions was called

Concept #4

"use strict";let name = "Ahmad Awais";let sayMyName = function() {
console.log(this.name);
};
sayMyName(); // undefined <== this is undefined.

ES6 TIP:

Hello, we’re the WordPress Couple!

--

--

Award-winning DevRel Eng. ❯ NodeCLI.com ❯ VSCode.pro ❯ Google Developers Expert ❯ Node.js Community Committee ❯ WordPress Core ❯ Self-confessed tech comedian!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmad Awais ⚡️

Award-winning DevRel Eng. ❯ NodeCLI.comVSCode.pro ❯ Google Developers Expert ❯ Node.js Community Committee ❯ WordPress Core ❯ Self-confessed tech comedian!