VBA->JS: Object basics and unnamed functions (Syntax & Concept)
Objects as a data type were introduced in a previous post. Now it’s time to look at them in more detail.
As mentioned before, people writing VBA code primarily work with the objects provided by the host application (the Office application’s object model). But sometimes you need to create one or more objects in order to work with something that’s not part of the Office application.
This can be done by defining a class. But probably the most common way this is done in VBA is by creating a User Defined Data Type at the beginning of a module “page”. This approach is much closer to what JavaScript (which doesn’t have classes) does.
There’s more than one way to create objects in JavaScript. I’ll start with the simplest, which has no equivalent in VBA. Then we’ll look at the approach that’s similar to a User Defined Data Type.
As an example, suppose you need to collect and use information about people within your code (as opposed to working with a database or external file or something similar). It would be useful to have an Object “Person” with properties such as FirstName, LastName and Birthday, as well as a way to return the full name and the age.
The straightforward way to create an object variable named “Person” with properties and methods in JavaScript looks like this:
var Person = { FirstName: "Jane", LastName: "Doe", Birthday: new Date(1999, 1, 0), FullName: function() { return this.FirstName + " " + this.LastName; }, Age: function() { var dtToday = Date(); var todayYear = new Date(dtToday).getFullYear(); return todayYear - this.Birthday.getFullYear(); } } Person.FullName(); //Result: "Jane Doe"
- The curly braces following the Equal sign tell JavaScript this is going to be an object.
- Then comes a property designation, followed by a colon, then the value to be assigned to the property.
- If another property should follow, you need a comma to separate them.
- In order to define a method, assign an unnamed (anonymous) function() to the property designation. The function syntax is otherwise the same.
- Note the use of the keyword “this” inside the functions in order to access properties already defined for the object. You can think of this the same way as “ThisDocument” or “ThisWorkbook” in VBA: it references the object of which it is a member.
- These examples make use of functions for working with Dates. You can find out more about Dates here. In this discussion,
new Date()
is used to assign date information (year, month, day) to a variable as a Date data type;Date()
by itself returns the current date and time;getFullYear()
returns the four-digit year of a Date object. - Note how dot (.) notation is used to reference an object’s properties and methods:
Person.FullName();
This is fine if you need a single object. But what about creating a number of objects of the same type? That’s what is normally done when using a User Defined Data Type in VBA:
VBA | JS |
---|---|
Type Person FirstName As String LastName As String Birthday As Date End Type Private Function GetAge( _ p As Person) As Long Dim dtToday As Date Dim dtBirthday As Date dtToday = Date dtBirthday = p.Birthday GetAge = DateDiff( _ "yyyy", dtToday, dtBirthday) End Function Sub PersonInfo() Dim p As Person p.FirstName = "Jane" p.LastName = "Doe" p.Birthday = "1.2.1999" Debug.Print GetAge(p) 'Returns 16 End Sub |
function Person( firstname, lastname, birthday) { this.FirstName = firstname; this.LastName = lastname; this.Birthday = birthday; this.FullName = function() { return this.FirstName + " " + this.LastName; }; this.Age = function() { var dtToday = Date(); var todayYear = new Date(dtToday).getFullYear(); var bdayYear = this.Birthday.getFullYear(); return todayYear - bdayYear; }; } var p = new Person("Joe", "Smith", new Date(1999, 1, 0)); p.Age(); //Returns 16 |
- One of the first things you’ll notice is that VBA cannot include methods as part of the Data Type; JavaScript can.
- No special keywords (
Type
) are involved in JavaScript: a named function (Person) to which parameters are passed is used to populate the object. - Notice how the syntax inside the Person function is similar, yet different compared to creating an object variable: the keyword “this” is used to define the property and method designations; an Equal sign assigns the values to them; each expression is followed by a semicolon.
- The reason for the line breaks in this example is to maintain a reasonable page width in the blog. Usually, property and variable assignments would not be put on separate lines.
As with VBA multiple objects of the same “type” can be “collected” in an array.
The next post will explore what else can be done with objects and functions.
October 20th, 2015 at 00:30
Why don’t I get an e-mail when you reply my comment? I easily forget what I comment =\
October 25th, 2015 at 18:29
Hi Felipe
I’ve made the correction and edited the comments to reduce “clutter” :-) Hope you’re OK with that.
About the email notifications: I don’t know, but I’ve had problems with the MVP WordPress site, myself, getting notifications. And there have been other problems. I’m seriously contemplating closing down the blog and sticking with my website…
October 16th, 2015 at 20:36
There is an extra pair of parentheses in bdayYear in the function below, I corrected it to:
function Person(
firstname, lastname, birthday) {
this.FirstName = firstname;
this.LastName = lastname;
this.Birthday = birthday;
this.FullName = function () {
return
this.FirstName + ” ” + this.LastName;
};
this.Age = function () {
var dtToday = Date();
var todayYear =
new Date(dtToday).getFullYear();
var bdayYear =
this.Birthday.getFullYear();
return todayYear – bdayYear;
};
}
var p = new Person(“Joe”, “Smith”,
new Date(1999, 1, 0));
p.Age();