Script Include Questions??

basantsoni
Kilo Guru

Hello Friends,

Can someone please answer some questions on Script Include:

1: What is difference between classless (on-demand) Script include & class Script Include, since I have seen some script include where we are directly using functions. Below are some cases:-

OOTB Script Include for Change Reqests.

var ChangeRequest = Class.create();

     

  1. ChangeRequest.NORMAL = "Comprehensive";
  2. ChangeRequest.STANDARD = "Routine";
  3. ChangeRequest.EMERGENCY = "Emergency";
  4. ChangeRequest.CHANGE_REQUEST = "change_request";

  1. ChangeRequest.prototype = Object.extendsObject(ChangeRequestSNC, {

      type: "ChangeRequest"

});

OOTB Script Include to validate email address.

function isNumeric(text) {

      if (text == null)

              return true;

      var validChars = "0123456789.,-";

      return containsOnlyChars(validChars, text);

}

function isInteger(text) {

      if (text == null)

              return true;

Regards,

Basant  

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Basant,



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.


View solution in original post

4 REPLIES 4

Kalaiarasan Pus
Giga Sage

This is a good blog about various patterns that are followed for script includes by tltoulson


https://codecreative.io/servicenow/interface-design-patterns-for-script-includes


bernyalvarado
Mega Sage

Super cool article by our friend Travis tltoulson. Thanks for sharing Kalai! kalai



Hi Basant,



The key difference in my opinion is a design one. A classless script include is intended for simple functions. Whenever i'm trying to prototype something quickly, i'll probably use a classless script include. It follows a structured programming paradigm. On the other hand, a class based script include will encapsulate the functions pertaining to an object. The instance of such object will allow you to handle through its life the various available methods. This become handy since internally to the class script include you can leverage the keyword this to make reference to the properties/attributes defined for the class. It's intended to follow object based programming without it been entirely object based programming (i'll not get in details here... that's more for a philosophy class or a good time among friends with a coffee cup ).



I hope this helps!



Thanks,


Berny


Harsh Vardhan
Giga Patron

Hi Basant,



Please check the link below.



TechNow Episode 6 | Script Includes - YouTube



Thanks,


Harshvardhan


tltoulson
Kilo Sage

Hi Basant,



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.