Class-based Script Includes create true object instances with state, unlike function-based ones that are just namespaces. The Class.create() pattern gives you constructor logic, private instance variables, and the ability to maintain state between method calls. The type property isn't decorative β it's required for proper instantiation, especially in scoped applications where the platform needs to resolve the class name correctly.
When to use this pattern
- When you need to maintain configuration or state between method calls on the same instance
- When building utilities that other developers will instantiate with
new ClassName()rather than static method calls - When you need constructor parameters to customize behavior per instance
- When building APIs that other scoped apps will consume β the
typeproperty ensures proper cross-scope instantiation
When NOT to use this pattern
- For simple utility functions with no state β use function-based Script Includes instead for better performance
- In client scripts β you need
Client callablechecked and GlideAjax pattern instead - When you only need static helper methods β creating instances adds unnecessary overhead
- For one-off logic that won't be reused β put it directly in the Business Rule or calling script
Key behaviors and gotchas
- The
typeproperty must exactly match the Script Include name β case sensitive - Missing
initializefunction causes silent failures when instantiating withnewoperator - Instance variables set in
initializewiththis.propertypersist across method calls on the same instance - Methods defined on
prototypeare shared across all instances β don't store instance-specific data there - Private methods prefixed with underscore are convention only β they're still accessible externally
- Cross-scope instantiation requires the Script Include to be in a dependency relationship or global scope
Forgetting the type property will cause 'ReferenceError: [ClassName] is not defined' when other scoped apps try to instantiate your class, even if the Script Include is accessible.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.