- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2023 05:59 PM
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();
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2023 10:26 PM
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.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2023 09:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2023 10:26 PM
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.
ServiceNow Community Rising Star, Class of 2023