What is 'this.' keyword in JavaScript

In JavaScript 'this' is an object and a reference to the current object that is used to get and to set properties of the current object.

Take for instance within a function;

const func = function (name){
    this.name = name
}

func.name instance property name is set to that of the first argument;

const inst = new func('duke')

inst.name
// returns duke

You can as well extend func to set name using 'this';

func.prototype.set = function(name){
    this.name = name
}

Running the previous instance with this set method;

inst.set('John')
// returns John

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript