May 2010 Meetup Minutes

By Ken Snyder, published 2010-05-07

Ken Snyder presented at the May 6th meeting on “Inheritance, Event Delegation and Named Function Expressions”

Named Function Expressions

Kangax has a great writeup exploring function declarations and function expressions: http://yura.thinkweb2.com/named-function-expressions/ . There are a few main differences:
  1. Declarations are in the form `function name() {}`. Expressions are part of a code expression such as assignment: `var name = function name() {}`
  2. Declarations are parsed immediately so the functions can be run before declared; they are parsed even if in closures or conditional statements. Expressions are parsed when encountered.
  3. Declarations are useless unless named. Expression functions can be unnamed and will show up in stack traces as `?` or `anonymous`; when named, the name is only defined within the function itself (except incorrectly in IE).

Prototypal Inheritance

Prototypal Inhertance is different than classical inheritance. Functions are king and inheritance is performed by setting the prototype property of a child class to a new instance of the parent class. See http://phrogz.net/js/classes/OOPinJS2.html and http://pulpjs.org/examples/#cls .

Event Delegation

In the JavaScript Document Object Model, events bubble up to parent elements. See http://utahjs.kendsnyder.com/delegate/ and the source code. The advantages include the following:
  1. When child content is changed (e.g. via AJAX), the event handlers do not need to be re-attached
  2. CSS selector engines can be used to call different handlers for different child elements
  3. There are fewer event handlers
One downside to watch for is many CSS selector engines including jQuery can be rather slow at identifying if the target element matches a given CSS selector. NWMatcher is a great selector engine that is incredibly fast because it works up from the target element instead of down from the source element or document.

About the Author

Name:
Ken Snyder
Location:
Lehi, UT
Github:
kensnyder
Twitter:
kencoder
Website:
http://kendsnyder.com/

Ken Snyder is a web developer specializing in PHP and JavaScript. He is one of the founding members of UtahJS. Ken's hobby is coding, so he gets to do his hobby all day. He currently works as a Senior PHP Developer at SolutionStream in Lehi, Utah.

About UtahJS.com

UtahJS is the website for the Utah JavaScript Users Group that meets to discuss and enjoy JavaScript!

JavaScript in the browser, on the server or just about anywhere else.

We love JavaScript but quite often talk about other topics such as CSS, HTML, cross browser coding, the internet… pretty much, anything that we find interesting.

Everyone is welcome. Please join us!