!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Lecture 10
JavaScript (part 4)
Ketan Mayer-Patel
University of North Carolina
Objects
- A collection of named properties and methods
- A method is a function associated with an object
- A class is a specific type of object.
- Array
- Date
- Function
- Document
- ...
- Classes organized into a hierarchy
- Object class is at the top of this hierarchy
Creating Objects
- From constructor of a specific class.
var d = new Date();
Generically as a literal
var my_obj = {prop1:value1, prop2:value2};
Instance
- What's an instance?
- A specific object
- More than one variable can be set to the same instance.
var a = new Date();
var b = a;
var c = b;
// Now all three variables refer
// to the same instance.
b = new Date();
// Now a and c refer to one instance,
// and b to another.
Using and defining methods
- A method is an object property that is set to a function object
- You call an object's method just like using a function.
- The this keyword
- Set to the object instance that the method belongs to.
- Creating a new method for a specific object
var o = {};
o.meth1 = function() {...
Example - creating and using object methods
Value Types vs Reference Types
- Value types
- Numbers, boolean, strings
- Passed to functions/methods by value.
- Value is copied when passed.
- Any changes not reflected in original value.
- Reference types
- Arrays and objects
- Passed to functions/methods by reference.
- Changes are reflected in original.
- Example - value vs. reference types.
JavaScript Readings and Resources
Assignment 3
- A3 out
- Will be catching up with grades tomorrow.