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...
- Javascript: The Good Parts by Douglas Crockford
- Douglas Crockford's note on objects in this short Survey of the JavaScript Programming Language from 2002
- An online version of Douglas Crockford's jsLint, a Javascript code quality tool
- An online version of jsHint, another Javascript code quality tool (developed as a fork of jsLint, as noted here)
- This stackoverflow post on object literals
- How to use Object Literals in JavaScript (excerpt from Javascript Patterns)
No comments:
Post a Comment