Private Function

Shubham Rai
Tera Contributor

 

What Is Private function ?

 

Can anyone help me ?

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @Shubham Rai ,

 

Private functions are the functions which can be accessed/called from same script include or child script include, which means it cannot be called in background script or Fix script directly. Private function in SN can be defined by using underscore in the beginning of function name e.g. _getUserData:function(){}

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare

View solution in original post

4 REPLIES 4

Astik Thombare
Tera Sage

Hi @Shubham Rai ,

 

Private functions are the functions which can be accessed/called from same script include or child script include, which means it cannot be called in background script or Fix script directly. Private function in SN can be defined by using underscore in the beginning of function name e.g. _getUserData:function(){}

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.

 

Regards,

Astik Thombare

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Shubham Rai 

 

Private functions are useful when you want to limit the scope of a function. You designate a function as private by storing it in a subfolder with the name private . Then, the function is available only to functions and scripts in the folder immediately above the private subfolder.

 

https://www.youtube.com/watch?v=arn5FApSR_g

 

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Shubham Rai ,

 

a private function is a function that is only accessible within the scope it is defined. It cannot be accessed or called from outside that scope, making it "private" to that particular code block or object.

Here's a basic example in Javascript:


function myPublicFunction() {
// This is a public function
}

(function() {
// This is an IIFE (Immediately Invoked Function Expression)

function myPrivateFunction() {
// This is a private function
}

// You can call myPrivateFunction only within this scope
})();

In the example above, `myPrivateFunction` is declared inside an Immediately Invoked Function Expression (IIFE). This function is private to the IIFE, and you cannot access it from outside that function. This pattern is often used to create private scopes and encapsulate functionality.

In the context of ServiceNow, you might encounter private functions when looking at client-side scripts, server-side scripts, or script includes where encapsulation and scoping are important for maintaining code organization and preventing unintended access or modification of certain functions or variables.

 

Thanks,

Danish

 

Sandeep Rajput
Tera Patron
Tera Patron

A Private function in script include can be prefixed with '_' and are usually unavailable to be called from a client script.