OLE Automation in JavaScript: What is Binding?
OLE Automation in JavaScript
What is Binding?
Before you can use the properties, methods, and events of an object model, you must first create a programmatic reference to the class containing the properties, methods, or events you wish to use. You do this by declaring a local object variable to hold a reference to the object. You then assign a reference of the object to the local variable.
VB and VBScript use the CreateObject()
function to enable and return a reference to an Automation object, while JScript uses the ActiveXObject()
constructor function.
Binding refers to the way in which Visual Basic code accesses objects in another application. When you use Automation from one application to work with objects in another application, the application in which you are writing Visual Basic code is the Automation controller. The application whose objects you are working with is the Automation server. When an Automation controller creates an object variable that points to an object supplied by an Automation server, Visual Basic must verify that the object exists and that any properties or methods used with the object are specified correctly. This verification process is known as binding. There are two types of binding with which Visual Basic developers need be concerned: late binding and early binding.
Late Binding
Late binding occurs at run time and is much slower than early binding. In late-bound Automation code, Visual Basic must look up an object and its methods and properties each time it executes a line of code that includes that object. To verify that the object and its methods and properties are specified correctly, Visual Basic must check with the operating system and with the application that supplied the object. Let's take a look at the following Visual Basic code segment:
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application")
The wdApp
variable is dimensioned as the generic type of Object
. When the variable is declared, Visual Basic doesn't know what type of object it will be, so it must set aside a certain amount of memory for this object. Since the specific object reference is assigned to the general variable, the application has no way of knowing in advance (at design time) what the makeup of the object interface is. Only at runtime does the application get to bind to the interface. Therefore, whenever you refer to the new object, Visual Basic must check the system registry for information on this object.
Early Binding
Early binding is the solution to the problem of slow Automation performance. Early binding occurs at compile time rather than run time, so if your code is saved in a compiled state, binding is complete before the code is even run. When using early binding, Visual Basic doesn't need to continually verify the object information while using the object during the execution of the application.
Not all Automation servers support early binding. The Automation server must provide a type library, containing information about the server's objects, methods, and properties. To take advantage of early binding, you must set a reference to the Automation server's type library. Visual Basic loads the type library into memory, which enables it to recognize these objects and bind them when the code is compiled. The following code segment shows how to create an early bound interface to an object:
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
Produced by Yehuda Shiran and Tomer Shiran
Created: January 10, 2000
Revised: January 10, 2000
URL: https://www.webreference.com/js/column55/binding.html