Are they the same?
The short the answer is Yes they are the same (in the sense they perform the same job of instantiating an automation object).
Basically unlike VBScript which has the global function CreateObject()
there is no such equivalent in JScript which was based on ECMAScript 3rd Edition. So, Microsoft added its own extension ActiveXObject
which does the same job as CreateObject
.
Both languages can be hosted in the Windows Scripting Host which gives them access to WScript.CreateObject()
which is another method that does exactly the same function but only in the context of the WScript
object that is only available through the Windows Scripting Host.
Following up
There has been some debate about whether they are the same, I still stand by my original answer they are the same. However, I will concede that I was comparing VBScript CreateObject()
and JScript new ActiveXObject()
not Wscript.CreateObject()
(which is slightly different).
Let's be clear though, all these functions and objects serve the same purpose which is to instantiate an automation object (COM). To back this up here is the official description of each;
WScript - CreateObject()
Method
Creates a COM object
JScript - ActiveXObject
Method
Enables and returns a reference to an Automation object
VBScript - CreateObject()
Function
Creates and returns a reference to an Automation object
If they were completely the same what would the point of them be? We already have language-specific automation instantiation methods, so what would the point of Wscript.CreateObject()
be?
The difference is when called with a second parameter it allows you to specify a prefix that will use to distinguish event handlers for that COM object.
Here is an example taken from this answer that shows how the second argument is used to set a prefix of objIE_
that will then be used to prefix any event handlers associated with that COM object, in this case, the InternetExplorer.Application
object.
// JScript
var objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true
while (objIE.Visible){
WScript.Sleep(500);
}
function objIE_NavigateComplete2(pDisp, URL){
WScript.Echo("You just navigated to", URL)
}
function objIE_OnQuit(){
boolBrowserRunning = false ;
}
It allows an Internet Explorer instance to be opened and the URL navigated to captured through the bound event, once the Internet Explorer Window is closed the script will end.
So while not identical they do perform the same function of instantiating an Automation (COM) object.
Useful Links