What is the significance of this keyword

Arun60
Tera Expert

Will anyone please help me to understand what is the significance of this keyword. Most of the time i see this keyword whenever we call server side call from client side[Glide Ajax]. please give some simple example for better understanding.

Thanks In Advance...

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

Hi Arun,



This refers to the current context object. For example within DateTimeUtils



var DateTimeUtils = Class.create();


DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


initialize: function() {


this.nowTime =   new GlideDateTime("02/21/2016");



},


msToGlideDateTime : function() {


          var ms = this.nowTime;


          var gDate = new GlideDateTime();


          gDate.setValue(parseInt(ms, 10));


          return gDate;


    }


);



If you notice in msToGlideDateTime, I am using this.nowTime defined above in initialize function. Here this refers to DateTimeUtils Object.


View solution in original post

2 REPLIES 2

venkatiyer1
Giga Guru

Hi Arun,



This refers to the current context object. For example within DateTimeUtils



var DateTimeUtils = Class.create();


DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


initialize: function() {


this.nowTime =   new GlideDateTime("02/21/2016");



},


msToGlideDateTime : function() {


          var ms = this.nowTime;


          var gDate = new GlideDateTime();


          gDate.setValue(parseInt(ms, 10));


          return gDate;


    }


);



If you notice in msToGlideDateTime, I am using this.nowTime defined above in initialize function. Here this refers to DateTimeUtils Object.


Hi Venkat



Thanks for your response. I have also find one example that might help others to understand the this variable concept .



      Point: -   You can add functions and properties to existing objects in this manner  


                                  The "this" variable is actually the GlideRecord object!



    Navigate to System Definition -> Script Include.


    Name: GlideRecordExtensions


    Script:


    GlideRecord.prototype.toObjectList = function() {  


    var objectList = [];  


     


      // loop through all the records and create the object array  


      while(this.next()) {  


              objectList.push(this.toObject(this));  


      }  


      this.restoreLocation(); // set it back to the beginning so that we can use if for other things  


      return objectList;    


      };


-----------------------------------------------------------------------------------------------------------------------------------------------------------


      gs.include('GlideRecordExtensions');  


 


      var incidents = new GlideRecord('incident');  


      incidents.addActiveQuery();  


      incidents.setLimit(10);  


      incidents.query();  


 


      gs.print(incidents.getRowCount());  


 


      Now invoke our new GlideRecord extension!  


      var stuffList = incidents.toObjectList();




Here this refers to incidents which is   a glide record object.


I hope this will help others to understand this concept