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 the 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 a memory leak, the performance of the 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,
The main advantage is to delete unwanted property from object,
  1. var automobile = {  
  2.     engine: true,  
  3.     fuel: true,  
  4.     tyres: 2  
  5. };  
  6. var car = Object.create(automobile); //car is instanceof automobile    
  7. console.log(car.tyres); // 2 it inherits from parent class    
  8. //as we know car has own tyres then we can create own property of car object    
  9. car.tyres = 4; // will make own tyre property of car object 
  1. //now we want to delete property tires from the car    
  2. delete car.tyres ; //will return true    

Q: We have deleted tyres from car object, what will happen if we print car.tyres?

The output of below statement will be 2 because it refer to tyres property in automobile object,
  1. console.log(car.tyres); // print 2    
  2. To delete tyres completely remove from automobile object  
  3. delete automobile.tyres; //true    
  4. 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.     this.engine = engine;  
  3.     this.fuel = fuel;  
  4.     this.tyres = tyres;  
  5. }  
  6. var car = new Automobile(truetrue, 4);  
  7. 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.
  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. 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 


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