Lecture notes - April 3
- Objects
var arthur = new Object();
arthur.quest = "Holy Grail";
arther.favoriteColor = "red";
- an abstract container for many kinds of data
- an object is actually a "reference" or pointer to something; not the thing itsef.
theKing = arthur;
doesn't copy the object - just gives it a new name.
- Example
- You can have objects within objects within objects ...
arthur.holyHandGrenade = new Object();
arthur.holyHandGrenade.count = 5;
arther.holyHandGrenade.goes = "boom";
- tree structure
- parent is object above ; children are objects below; siblings are objects nearby in the tree
- The point of all this is "managing complexity" by hiding some parts within other parts.
- Each browser window has a built-in heirarchy like this one, full of data and methods.
- A "method" is a part of an object that does something; they're always follwed by parens.
- Mozilla has a DOM inspector Tool that can look at all these built-in object trees.
- They get complicated - and are somewhat different from browser to browser.
- But you can use them to do some pretty cool stuff.
- Since an object is a "pointer", they can even point to
themselves, giving what appear to be circular structures. (See
document.defaultView.... in DOM tool.)
- the "window" object is the parent of the whole DOM tree.
- all objects defined in javascript within a window are children of window
- but since it's so common, that it's name is implied
- even "arthur" is a part of this object; see the DOM tool.
- one important object : document (actually window.document)
- document.write(...) we've already seen
- document.width
- document.height
- document.bgColor
- document.fgColor
- document.lastModified
- Put these together with if(){...} to make a page that is different colors for different widths!
- other object methods mentioned in the text
- window.location (the URL address)
- window.location = ... (jump to a new page)
- window.open("newFile.html", "name", width=300,height=400) - a pop-up !
- JavaScript has some built-in objects, like Math
- x = Math.PI
- y = Math.random()
- z = Math.round(x)
- - quick quiz : why does random have the () after it, but Math.PI doesn't ?
- explore browser objects in class with DOM tool