Voice of a Developer: Part Nine - JavaScript Useful Reserved Keywords

JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.

Before moving further let us look at the previous articles of the series:

JavaScript has many useful reserved keywords. I will walk you through usage and benefits of these keywords

Delete keyword

When we work on large applications then we work with many objects. We are familiar with problems like memory leak, performance of application is not good, and similar issues arise at a later stage. I always recommend that we know how much memory we are consuming in our application.

Ex- Chrome provides a quick way to look into memory usage,
code
The main advantage is to delete unwanted property from object,

  1. var automobile = {engine: true, fuel: true, tyres:2 };  
  2. var car = Object.create (automobile); //car is instanceof automobile  
  3. console.log (car.tyres); // 2 it inherits from parent class  
  4. //as we know car has own tyres then we can create own property of car object  
  5. car.tyres = 4; // will make own tyre property of car object  
diagram
  1. //now we want to delete property tyres from car  
  2. delete car.tyres ; //will return true   
Q: We have deleted tyres from car object, what will happen if we print car.tyres?

Ans: The output of below statement will be 2 because it refer tyres property in automobile object,
  1. console.log (car.tyres); // print 2  
  2.   
  3. To delete tyres completely remove from automobile object  
  4.   
  5. delete automobile.tyres; //true  
  6. console.log (automobile.tyres); // print undefined  
This keyword

“this” is not a variable. It is a keyword. You cannot change the value of this. It refers to the global object in all global code.

We know to create multiple objects we can use Object Constructor function. We’ll use this keyword to create property which belong to function,
  1. function Automobile (engine, fuel, tyres)  
  2. {  
  3. this.engine = engine;  
  4. this.fuel = fuel;  
  5. this.tyres = tyres;  
  6. }  
  7. var car =new Automobile (truetrue , 4);  
  8. var bike =new Automobile (truetrue , 2);  
Second example, this refers to the parent object in the body of the function,
  1. var counter = {  
  2.   val: 0,  
  3.   increment: function () {  
  4.     this.val += 1;  
  5.   }  
  6. };  
  7. counter.increment();  
  8. console.log(counter.val); // 1  
  9. counter['increment']();  
  10. console.log(counter.val); // 2  
New operator

The new operator creates a new object. Any JavaScript function can be used as a constructor function with new.

Example
  1. function F1 (val) {  
  2.   this.val = val;  
  3. }  
  4. var f = new F1("Foo");  
  5. console.log(f.val); // Foo  
  6. console.log(this.val); // ReferenceError: val is not defined. Because val is associated with function object  
  7.   
  8. If we call f without new then this will refer to the global object (which is ex Windows )  
  9. function F1 (val) {  
  10.   this.val = val;  
  11. }  
  12. var f = F1("Foo");  
  13. console.log(this.val); // Foo  
Summary

Rules are simple just we need awareness how to use delete, this, and new. Please share comments and feedback.
 
Read more articles on JavaScript

 

Up Next
    Ebook Download
    View all
    Learn
    View all