T1000Android Posted March 15, 2013 Share Posted March 15, 2013 (edited) Hi guys! I found this code on the internet the other day. The code works fine: it enumerates the methods of the document object. <script type="text/javascript">function getMethods(obj) { var result = []; for (var id in obj) { try { if (typeof(obj[id]) == "function") { result.push(id + ": " + obj[id].toString()); } } catch (err) { result.push(id + ": inaccessible"); } } return result;}alert(getMethods(document).join("\n"));</script> What I don't understand is what's that on the third line? I know that "var result = [];" is a variable named "result" but what's that it's equal to? What does the array operator mean when given as a value to a variable? Edited March 15, 2013 by T1000Android Link to comment Share on other sites More sharing options...
Ingolme Posted March 15, 2013 Share Posted March 15, 2013 Square brackets are a shorthand representation of an array. If there's nothing between them then it represents an empty array. It's equivalent to writing var result = new Array(); except it's a little more efficient in most browsers. Link to comment Share on other sites More sharing options...
T1000Android Posted March 15, 2013 Author Share Posted March 15, 2013 Thanks. I panicked for a second there. Link to comment Share on other sites More sharing options...
thescientist Posted March 16, 2013 Share Posted March 16, 2013 Also, you can tell its an array because in the loop, the push method is being called on it. Link to comment Share on other sites More sharing options...
T1000Android Posted March 16, 2013 Author Share Posted March 16, 2013 Thanks scientist, but I'm not familiarized with the methods since I'm new to this. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now