神刀安全网

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

JavaScript is language of Web. This series of articles will talk about my observation, learnt 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,

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

The main advantage is to delete unwanted property from object,

var automobile = {engine: true, fuel: true, tyres:2 };   var car = Object.create (automobile); //car is instanceof automobile   console.log (car.tyres); // 2 it inherits from parent class   //as we know car has own tyres then we can create own property of car object   car.tyres = 4; // will make own tyre property of car object   

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

//now we want to delete property tyres from car   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,

console.log (car.tyres); // print 2      To delete tyres completely remove from automobile object      delete automobile.tyres; //true   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,

function Automobile (engine, fuel, tyres)   {   this.engine = engine;   this.fuel = fuel;   this.tyres = tyres;   }   var car =new Automobile (true, true , 4);   var bike =new Automobile (true, true , 2);   

Second example, this refers to the parent object in the body of the function,

var counter = {     val: 0,     increment: function () {       this.val += 1;     }   };   counter.increment();   console.log(counter.val); // 1   counter['increment']();   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

function F1 (val) {     this.val = val;   }   var f = new F1("Foo");   console.log(f.val); // Foo   console.log(this.val); // ReferenceError: val is not defined. Because val is associated with function object      If we call f without new then this will refer to the global object (which is ex Windows )   function F1 (val) {     this.val = val;   }   var f = F1("Foo");   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

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Voice of a Developer: Part Nine – JavaScript Useful Reserved Keywords

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址