
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018 08:44 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018 09:03 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018 08:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018 09:03 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2018 09:19 PM
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.