Access Javascript methods with parentheses?

I saw this in some code:

var _0xdf50x7 = document['createElement']('form');

How it works? Does this mean that object methods can be accessed as elements of an array?

+3
javascript
source share
1 answer

Since the createElement() method is a member of the document object, it can be accessed using either dot notation :

 var form = document.createElement("form"); 

Or parenthesis designation :

 var form = document["createElement"]("form"); 

This can be useful if the name of the method to call is stored in a variable:

 var methodName = "createElement"; var form = document[methodName]("form"); 

It can also be used if the actual invocation method is dependent on external conditions. Here is an example (far-fetched):

 function createNode(str, isTextNode) { return document[isTextNode ? "createTextNode" : "createElement"](str); } 
+5
source share

All Articles