"This" keyword in extended script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 09:07 AM
When extending a script include, does the keyword "this" only apply to the current script include or can it also access methods in the extended script include?
For example, I'm extending the hr_Case script include and wanting to know if I can do something like this:
// hr_Case script include example
var hr_Case = Class.create();
hr_Case.prototype = {
isApproverUserForCase : function(userId){
var sysApprovalGR = new GlideRecord('sysapproval_approver');
sysApprovalGR.addQuery('sysapproval', this._case.getUniqueValue());
sysApprovalGR.addQuery('approver', userId);
sysApprovalGR.query();
return sysApprovalGR.next();
},
type : "hr_Case"
};
// new script include extending hr_Case
// returning the output using this.isApproverUserForCase(userId)
var CS_HRCaseACL_Utils = Class.create();
CS_HRCaseACL_Utils.prototype = Object.extendsObject(sn_hr_core.hr_Case, {
//Override to check for subject person access, if can edit case or is approver
_evaluateROConditionForHRCase: function(userId){
return this.isApproverUserForCase(userId);
},
type : "CS_HRCaseACL_Utils"
});
Would the _evaluateROConditionForHRCase method in the script include that's extending hr_Case return the proper value with how it's written?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 09:49 AM
Hello Justin,
The difference is actually very subtle. The two paradigms in play are Prototype Based Inheritance vs Closures. I say the difference is subtle because you can create Object-Oriented style classes with either approach. A closure can return new instances just the same as those using a prototype.
The main difference between the two is the ability to flexibly share behavior (inheritance) vs information hiding (encapsulation)
1. Prototype based classes (Class based) make inheritance easy by inheriting and overriding through the prototype but the use of the this keyword makes encapsulation difficult because all properties shared in the objects functions have to be public.
2. Closure based objects (Classless) place an emphasis on encapsulation where all members are private to the closure unless explicitly made public. But it does this at the cost of inheritance. Closures don't leverage a prototype chain, so each object has to have a copy of its own functions.
But unfortunately, while extremely helpful, this is also overly simplistic. Javascript is a (very) dynamic language. We can make a prototype based class' initialize function a closure to get information hiding. We can also manipulate and return object prototypes within a closure or add a prototype to a closure to obtain inheritance. This mix and match allows us to add exactly the features we desire when we want them.
https://codecreative.io/blog/interface-design-patterns-for-script-includes/
Accessing 'current' is a bad idea in script includes. The idea of a script include function/method is that it can be called from anywhere in server side code (business rule, UI action, background script, etc.) Not all of these offer the 'current' object. What's more, many of those are now encapsulated in functions so current's scope is private.
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2019 12:51 PM
Hello Abhishek,
Thank you for your response. I'm still a little unclear on whether or not my example should work. I believe it does but I'm having some trouble when testing it out/troubleshooting, so was hoping for some validation on whether or not the code should work. Maybe I missed it in your answer but please let me know your thoughts.
Thanks!
Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2019 01:10 PM
Please review this link. Shows you how to use inheritance and function calls. Please pay attention to the scopes of the Script includes.
https://codecreative.io/blog/interface-design-patterns-class-pattern/
Vinod Kumar Kachineni
Community Rising Star 2022