Difference between way of Initializing the script include

Aman Gurram
Giga Expert

What is the difference between the below two ways of instantiating an object to a script include (class)

Way 1:

var obj = new scriptIncludeName();
obj.functionName();

Way 2:
var obj = scriptIncludeName.create();
obj.functionName();

 

Both methods do the same thing, but is there any real difference between them. 

Please correct me If I am wrong.

1 ACCEPTED SOLUTION

Ricardo Velez
ServiceNow Employee
ServiceNow Employee

Hello Agurram,

 

The correct and documented way to instantiate a script include is as follows:

 

var foo =new NewInclude();
foo.myFunction();

The second option you present is meant to act as the class creator which is presented when creating a new script include.
Class.create() creates a class and returns a constructor function for instances of the class.

A script include is very similar to the representation of a java class. To instantiate a new object in Java, you use the 'new' operator.

The correct way to do this therefore is the first one.

For further documentation please refer to the following link:

https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/con...

Best regards,
Ricardo Velez | Senior Technical Support Engineer
ServiceNow | The Enterprise Cloud Company

View solution in original post

3 REPLIES 3

Deepak Ingale1
Mega Sage

When you use new , you instantiate the object and initialize function defined in script include gets executed. 

You can define common variables or intialization variables inside initialize function.

When you try to access the object without initializing it ( without new prefix) , "initialize()" function does not execute.

 

Hope this answers your query

Ricardo Velez
ServiceNow Employee
ServiceNow Employee

Hello Agurram,

 

The correct and documented way to instantiate a script include is as follows:

 

var foo =new NewInclude();
foo.myFunction();

The second option you present is meant to act as the class creator which is presented when creating a new script include.
Class.create() creates a class and returns a constructor function for instances of the class.

A script include is very similar to the representation of a java class. To instantiate a new object in Java, you use the 'new' operator.

The correct way to do this therefore is the first one.

For further documentation please refer to the following link:

https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/con...

Best regards,
Ricardo Velez | Senior Technical Support Engineer
ServiceNow | The Enterprise Cloud Company

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

 

var obj = new scriptIncludeName();
obj.functionName();

Answer:

When we use this then Script include Object is created and only specific function gets called specified in line two.

 

var obj = scriptIncludeName.create();
obj.functionName();

Answer:

When we use this then Script include Initialize() function is called, in this function we initialize some other script includes and variables which can be used in other functions and then function name specified.

 

Thanks,

Ashutosh Munot

 

Please Hit Correct, Helpful or like,if you are satisfied with this response.