Sunday, November 24, 2013

Javascript Objects: Just Bags of Stuff

I have been reading a smorgasbord of javascript articles lately. It's really nice how little pieces of understanding can sometimes come together gradually as I do this.

Javascript is deceptive because it is so simple. One of the (surficial) simple things about javascript is its notion of an object.

A javascript object is simply a "bag" of things (technically, it's just a hashtable). These other things can be numbers, strings, functions, and other objects. You access them by a name you have given them.

That's it.

Here's a simple code snippet:

  var myObject = {"myProperty": 42};

This defines an object named myObject that has a single property named "myProperty", which has value "42".

Defining an object this way is referred to as using an "object literal".

So what is created when you create myObject this way? Here's a tiny html page

<!DOCTYPE html>
<html>
<head>
<script>
  var myObject = {"myProperty": 42};
  console.dir(myObject);
</script>
</head>
</html>

and here's what's in the Chrome console when you load this page and click on the "Object":



There is our custom property "myProperty", but there is also something else there that we did not define ourselves: the "__proto__" property. Some info on this is here.


For More Reading...


No comments:

Post a Comment