Lecture 3 (9/11): Javascript Objects and Flocking

by Mike Gleicher on September 9, 2012

Goals for today:

  • A little more about Javascript – specifically objects (since we didn’t get to it last time, and it came up in an unsatisfying way in lab)
  • A little bit about flocking (since you can read more)

Javascript Objects are weird

  • Prototype/Delgation vs. Class/Inheritence
  • But not really P/D
  • And some weird functionalness thrown in

Prototypical vs. Classical Object Oriented

  • P/L theory makes a bigger deal of this than it is
  • Meta-Class Mammal, Class Cat, instance Morris, Indigo
    • Cat is a different thing than Morris/Indigo
    • You can do different things with one
  • Prototype
    • Morris (speaks() “meow”, name: morris, legs:4, color: orange)
    • Fluffy: (prototype = Morris)
    • delegation vs. copying (behavior change) (speak->purr)
  • Sane ways to make prototypes
    • new Object + set prototype
    • clone / modify

Javascript object construction

  • new! Object.create(prototype) – seems much more reasonable!
  • Constructor function (sets up a new object)
  • Has a “prototype” (for the new objects it sets up)
  • takes a generic object as “input” turns it into an instance
  • weird new operator
    • when a constructor is called with new, this is set correctly
    • when a constructor is not called with new, this is not set correctly
   1: function makeBall(x,y) {

   2:     // Empty = function () {};

   3:     // Empty.prototype = aBall;    // don't ask why not ball.prototype=aBall;

   4:     // ball = new Empty();

   5:     ball = Object.create(aBall);

   6:     ball.x = x;

   7:     ball.y = y;

   8:     return ball;

   9: }

Methods

  • define functions as object variables
  • can’t do closures with literals (only functions create scopes)
  • use this to refer to owning object (left of the dot in method invocation)
  • allows methods to be added later

Constructor Functions

  • was the only way
  • assumed that would only be called with the “this” method
  • has a prototype property
  • can set up the object (but not the methods!)
  • Constructor.prototype.method = function(), default value, …

Simulation basics

see http://graphics.cs.wisc.edu/Courses/Games08/Main/Flocking

see https://pages.graphics.cs.wisc.edu/679-11/files/2011/09/09-13-11-GamesFlocking.pdf (pages 3/4)

  1. 2008.1 (concept of local control, historical note, terms)
  2. particles / simulation (state, velocity, why this is ODE, step model) 2011 notes
  3. 2008.2 (basic model)
  4. 2008.3 (more flocking stuff)
Print Friendly, PDF & Email

Previous post:

Next post: