Jump to content

What's that?


T1000Android

Recommended Posts

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 by T1000Android
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...