Skip to content

Commit b5d4dda

Browse files
committed
modification to getRoleMember() function
1 parent a7be10e commit b5d4dda

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

dci/dci.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ if (!isNodeJs) {
3030
//DCI.getRoleMember(__context, __context.DestinationAccount, 'DestinationAccount', 'de' + 'posit').call(__context.DestinationAccount, amount)
3131
//Gets a member on a role player - can be either a role method or a method or property of the role player object
3232
function getRoleMember(context, player, roleName, memberName) {
33+
if (player != context[roleName]) {
34+
//If we're here, it's because the programmer used `this` inside a closure inside a role method.
35+
//So either `this` refers to some other object besides the current role, or the programmer used `this`
36+
//inside a closure when they should have used `self`.
37+
//
38+
//In other words this code would also be reached if `this` is equal to `undefined`, `global`, or `window`.)
39+
//...for example, if the SourceAccount.transferOut() method in the Transfer Money example contained the following code:
40+
// [1,2,3].forEach(function() {
41+
// this.withdraw(); //`this` is actually equal to `window` or `global` here! (or `undefined` in strict mode)
42+
// });
43+
//
44+
//Because we need to account for the first case (`this` refers to some other object besides the current role),
45+
//which is perfectly valid, we simply return the property on `this` just as would happen normally in Javascript.
46+
return player[memberName];
47+
}
48+
3349
var roleMethod = context['__$' + roleName][memberName];
3450
if (roleMethod) {
3551
//bind the role player as `this` on the specified role method

dci/dci.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ if (!isNodeJs) {
9191

9292
//Gets a member on a role player - can be either a role method or a method or property of the role player object
9393
export function getRoleMember(context: Object, player: Object, roleName: string, memberName: string) {
94+
if (player != context[roleName]) {
95+
//If we're here, it's because the programmer used `this` inside a closure inside a role method.
96+
//So either `this` refers to some other object besides the current role, or the programmer used `this`
97+
//inside a closure when they should have used `self`.
98+
//
99+
//In other words this code would also be reached if `this` is equal to `undefined`, `global`, or `window`.)
100+
//...for example, if the SourceAccount.transferOut() method in the Transfer Money example contained the following code:
101+
// [1,2,3].forEach(function() {
102+
// this.withdraw(); //`this` is actually equal to `window` or `global` here! (or `undefined` in strict mode)
103+
// });
104+
//
105+
//Because we need to account for the first case (`this` refers to some other object besides the current role),
106+
//which is perfectly valid, we simply return the property on `this` just as would happen normally in Javascript.
107+
return player[memberName];
108+
}
109+
94110
var roleMethod = context['__$' + roleName][memberName];
95111
if (roleMethod) {
96112
//bind the role player as `this` on the specified role method
@@ -105,8 +121,6 @@ export function getRoleMember(context: Object, player: Object, roleName: string,
105121
}
106122
}
107123

108-
109-
110124
//This function is for handling calls beginning with `this`; it calls a method on the current role player,
111125
//which could be either a role method or a method on the data object.
112126
//

0 commit comments

Comments
 (0)