Can anybody please explain why we initialize Object using this? What is the significance? Code below

EKTA2
Tera Expert

Please help me understand the significance of initializing with this keyword here. Even without this the code will work, if we pass the parameters to function ( assuming function is returning something).. 

 

 

SPCart.prototype.initialize = function(cartName, userID) {
this.cartName = !cartName ? null : cartName;
this.userID = !userID ? null : userID;

this.cart = this.getCart();
};

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage
Mega Sage

@EKTA2 

The initialize function is a constructor for the script include. Hence the parameters passed in the script include call can be accessed in initialize function.

 

Please check the below example.

 

var spCart = new SPCart("TEST CART NAME", gs.getUserID());

 

The 2 parameters passed from above script include call will be passed to initialize function  and are stored in "this.cartName" and "this.userID" variables. These variables can be used in any function of the script include.

 

Now "this.getCart();" function is called and the response from it is stored in "this.cart" variable.

 

"this.cart" can be used in any function of script include as "this.cartName" and "this.userID" variables and also in the script from where the script include is called like below

 

spCart.cartName

spCart.userID

spCart.cart

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

2 REPLIES 2

ChrisBurks
Mega Sage

I'll give it a try:
In this context "this" is going to refer back to the SPCart object. For me, I might assume that the SPCart object has the properties cartName, userID and cart. If that is true, when the initialize method is invoked it will change the values of those properties with the results from the initialize method.
If it isn't true that the properties exist in SPCart then the properties and their results will be added to SPCart after the initialize method is invoked.

References: this keyword in javascript-  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Javascript inheritance and prototype: -  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

jaheerhattiwale
Mega Sage
Mega Sage

@EKTA2 

The initialize function is a constructor for the script include. Hence the parameters passed in the script include call can be accessed in initialize function.

 

Please check the below example.

 

var spCart = new SPCart("TEST CART NAME", gs.getUserID());

 

The 2 parameters passed from above script include call will be passed to initialize function  and are stored in "this.cartName" and "this.userID" variables. These variables can be used in any function of the script include.

 

Now "this.getCart();" function is called and the response from it is stored in "this.cart" variable.

 

"this.cart" can be used in any function of script include as "this.cartName" and "this.userID" variables and also in the script from where the script include is called like below

 

spCart.cartName

spCart.userID

spCart.cart

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023