This Operator in Programming

Shubham Bartia
4 min readNov 13, 2020

One of the most confusing topics in any programming language for developers to understand can be understanding the “this” operator, so keep that in mind I will try my very best to make you clear on “this” operator.

Definition of this operator

The “this” operator is used to refer to the current object. In general, this refers to the calling object in a method.

Syntax

this.propertyName

Variable Scope: Global vs Local

For a better view of “this” operator, let us know the concept of variable scope in programming. A variable’s scope is the range of the script where it is visible. So there are two scopes in a declaration basically local and global scope.

Global Variable

A global variable exists only once in a script and is visible in every function. Modifications to it in one function are permanent and visible to all functions. Unless declared otherwise, all variables in a script are global. Global variables are useful for values that are relatively constant, or that many functions in the script must access, such as a session id.

Example of global and scope:

<A NAME=main>
let x = "fragile" <!-- global $x -->
<LOCAL x>
let x = "test" <!-- only local $x seen here -->
...
</LOCAL>
console.log("The value of x is: " +x) <!-- back to global $x -->
</A>

The above example clearly shows the difference between the two scopes, the global variable are been accessible by any method and can be used anywhere in the program while the local variable scope is been completely been restricted to its method.

It exists only within the block that it is declared in. Once that block ends, the variable is destroyed and its values lost. A local variable of the same name declared elsewhere is a different variable. A local variable can even exist multiple times simultaneously if its block is entered again before it’s exited — i.e. a recursive function call. Each call of the function will have a distinct local variable.

Local variables must be explicitly declared, either as parameters to a script function or with the LOCAL statement. They are used to clearly pass parameters to functions, or as temporary "scratch space" for a function without the side effects of global variable modification.

Consider it in this way, imagine an item kept outside anywhere, and anyone who wants to use the item kept on the outside can easily use it as it is openly available, hereby this example I am referring to the global variable which can be accessed very easily due to being global in nature.

Now consider an item inside a box, do to think it is easily accessible or anyone can easily access it, well no, as it is inside a container and no one knows what's inside so it is been restricted to only people to whom the box belongs to. This is an example of a local variable which are restricted to its methods limits.

How can access a local variable globally?

Or in this case, how can we make efficient use of the box so even that can be used globally. So to resolve this problem let me introduce “this” operator finally. The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). Let me explain via programming example.

private:int x; // global varibalepublic:void setX (int x){// The 'this' pointer is used to retrieve the object's x// hidden by the local variable 'x'this->x = x; // reading the local data and storing in global    variable}void print() { cout << "x = " << x << endl; }};

Here in the above example, you can see that global data is accessed via,(this->x) data, and the data which came in the local variable can be given to global data. In this way, the data even inside a method scope can be used globally by giving the data present or can be used only locally inside a function to be used globally using “this” operator.

Conclusion

The above was just a small demo or trail on understanding the concept of “this” operator, so next time on declaring a variable to go glocally just recall the item inside a box example, so items outside easily available are global accessible while data inside the box has a limit to the user known the box and is not accessible to everyone. To make the content be useful globally “this” operator is used in programming. The more you code, the more you learn. Just remember programming is not just a job, it's an art. Happy Coding.

--

--

Shubham Bartia

Full-stack Web Developer, loves to travel, party, read, write, crowd speaking and code