3

I usually program in Python, but today I was trying to reverse engineer some Javascript code and I came across this line:

var ABC = DEF[XYZ];

DEF is a function that was defined earlier. It takes one argument. XYZ is a string that was created earlier, and it contains the results of running DEF previously.

I don't know much JavaScript, but it seems to me like it is defining a variable called ABC that contains the results of running function DEF with the argument XYZ. However, later on there is a line that goes var GHI = ABC(JKL, DEF(MNO)) (JKL and MNO are both variables defined earlier).

What puzzles me is that ABC was defined as a variable, not a function. So I'm thinking this has something to do with the square brackets seen earlier.

So my question is: what is the purpose of those square brackets?

5
  • DEF is not a function, pretty sure. Can you show the source of DEF? You need to show us the context. If you can't link the exact code, cut it down and make one that behaves similarly. Commented Mar 28, 2017 at 22:32
  • 1
    Searching for: JavaScript bracket would have led you to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… So it is referencing a property in an object and that property contains a function. Commented Mar 28, 2017 at 22:32
  • Functions are objects and [...] is used to get a property of an object. So var XYZ = "call"; var ABC = DEF[XYZ]; is the same as var ABC = DEF.call Commented Mar 28, 2017 at 22:32
  • ...ultimately you can use development tools to see what values are produces from different expressions. Commented Mar 28, 2017 at 22:35
  • JavaScript could generate a warning message. I just found a bug in my code where I defined a function foo and tried to get its value by writing the expression foo[x]. This kept returning "undefined" and it took me about twenty minutes to see that I had used [] instead of (). Commented Dec 30, 2022 at 23:14

1 Answer 1

4

what is the purpose of those square brackets?

To access a member of an object.

It's not calling a function. It's attempting to access a member of the object DEF (without seeing the rest of the code, it's impossible to tell what kind of object DEF is, as you can use object member accession like that for any type of object that contains members, like plain objects, functions, arrays, etc.) For example:

var DEF = {
  foo: 'bar'
};

var XYZ = 'foo';

var ABC = DEF[XYZ];

console.log(ABC); // => 'bar'

As another example, DEF could also be an array, and if XYZ is a number, then it's accessing a specific index of that array.

Update

If DEF is a function, you can still access members of it:

function DEF(arg) {
  // do something with arg
}

DEF.foo = function(str) {
   console.log('I am doing ' + str)
}

var XYZ = 'foo'

console.log(DEF[XYZ]) // => function() {}

var ABC = DEF[XYZ]

ABC('yoga') // => 'I am doing yoga'

You can call DEF[XYZ](someArgument) like this.

Sign up to request clarification or add additional context in comments.

20 Comments

Basically, DEF is a function in the code that takes in a string as an argument, modifies the string, then returns the modified string.
@ejj28 the square brackets don't call a function. If you wanted to call DEF as a function, you'd call DEF(someArgument)
Yes, I understand that much. I didn't write this code, it's a piece of malware that I'm trying to analyze. DEF is called normally a few times before the line with the square brackets. I am just confused as to what the square brackets are doing.
@ejj28 the square brackets access a member of the function :) some property. See my updated answer.
I managed to reverse-engineer the code without figuring out that line. Thanks so much for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.