Top Banner
Anything But Typical Learning to Love JavaScript Prototypes Page 1 © 2010 Razorfish. All rights reserved. Dan Nichols March 14, 2010
62

Anything But Typical

Feb 24, 2016

Download

Documents

Austin Kim

Anything But Typical. Learning to Love JavaScript Prototypes. Dan Nichols March 14 , 2010. JavaScript. Object-Oriented Programming. class. Inheritance. prototype (not the library). The story so far…. . …except it wasn’t all good. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Anything But Typical

Anything But TypicalLearning to Love JavaScript Prototypes

Page 1 © 2010 Razorfish. All rights reserved.

Dan Nichols

March 14, 2010

Page 2: Anything But Typical

JavaScript

Page 2 © 2010 Razorfish. All rights reserved.

Page 3: Anything But Typical

Object-Oriented Programming

Page 3 © 2010 Razorfish. All rights reserved.

Page 4: Anything But Typical

class

Page 4 © 2010 Razorfish. All rights reserved.

Page 5: Anything But Typical

Inheritance

Page 5 © 2010 Razorfish. All rights reserved.

Page 6: Anything But Typical

prototype(not the library)

Page 6 © 2010 Razorfish. All rights reserved.

Page 7: Anything But Typical

The story so far…

Page 7 © 2010 Razorfish. All rights reserved.

Page 8: Anything But Typical

<a onclick=“javascript:alert(‘chaos!’)”>

Page 8 © 2010 Razorfish. All rights reserved.

Page 9: Anything But Typical

Page 9 © 2010 Razorfish. All rights reserved.

Page 10: Anything But Typical

…except it wasn’t all good.

Page 10 © 2010 Razorfish. All rights reserved.

Page 11: Anything But Typical

Page 11 © 2010 Razorfish. All rights reserved.

Page 12: Anything But Typical

Page 12 © 2010 Razorfish. All rights reserved.

Page 13: Anything But Typical

Page 13 © 2010 Razorfish. All rights reserved.

Page 14: Anything But Typical

Page 14 © 2010 Razorfish. All rights reserved.

Page 15: Anything But Typical

Sound familiar?

Page 15 © 2010 Razorfish. All rights reserved.

Page 16: Anything But Typical

Page 16 © 2010 Razorfish. All rights reserved.

prototype (not the library) to the rescue!

Page 17: Anything But Typical

Page 17 © 2010 Razorfish. All rights reserved.

Everything is an object

Page 18: Anything But Typical

Page 18 © 2010 Razorfish. All rights reserved.

Every object has a prototype

Page 19: Anything But Typical

Page 19 © 2010 Razorfish. All rights reserved.

prototypes do good things

Page 20: Anything But Typical

Page 20 © 2010 Razorfish. All rights reserved.

Encourage modularityand reusability

Page 21: Anything But Typical

Page 21 © 2010 Razorfish. All rights reserved.

Provide a consistent wayto do (different) things

Page 22: Anything But Typical

Page 22 © 2010 Razorfish. All rights reserved.

Keep code to a minimum

Page 23: Anything But Typical

Page 23 © 2010 Razorfish. All rights reserved.

Make complexity easy

Page 24: Anything But Typical

What is a prototype, exactly?

Page 24 © 2010 Razorfish. All rights reserved.

Page 25: Anything But Typical

Page 25 © 2010 Razorfish. All rights reserved.

A reference point for other objects

Page 26: Anything But Typical

Property lookup chain

Page 26 © 2010 Razorfish. All rights reserved.

Page 27: Anything But Typical

Property lookup chain

Page 27 © 2010 Razorfish. All rights reserved.

instance

Date

Object

Page 28: Anything But Typical

Property lookup chain

Page 28 © 2010 Razorfish. All rights reserved.

instance

Date

Object

Page 29: Anything But Typical

Property lookup chain

Page 29 © 2010 Razorfish. All rights reserved.

instance

Date

Object

Page 30: Anything But Typical

Property lookup chain

Page 30 © 2010 Razorfish. All rights reserved.

instance

Date

Object

Page 31: Anything But Typical

Page 31 © 2010 Razorfish. All rights reserved.

JavaScript is different frommost other OOP languages

Page 32: Anything But Typical

Anatomy of a prototype

Page 32 © 2010 Razorfish. All rights reserved.

rex

Poodle

Dog

Object

Page 33: Anything But Typical

Anatomy of a prototype

Page 33 © 2010 Razorfish. All rights reserved.

Constructor

Page 34: Anything But Typical

Anatomy of a prototype

Page 34 © 2010 Razorfish. All rights reserved.

Property

Method

Page 35: Anything But Typical

Anatomy of a prototype

Page 35 © 2010 Razorfish. All rights reserved.

Constructor

Inheritance

Page 36: Anything But Typical

Anatomy of a prototype

Page 36 © 2010 Razorfish. All rights reserved.

Instance

Instance Property

Page 37: Anything But Typical

this and the lookup chain

Page 37 © 2010 Razorfish. All rights reserved.

fluffy

Cat

Object

socks

Page 38: Anything But Typical

this and the lookup chain

Page 38 © 2010 Razorfish. All rights reserved.

socks

Cat

Object

Page 39: Anything But Typical

this and the lookup chain

Page 39 © 2010 Razorfish. All rights reserved.

fluffy

Cat

Object

Page 40: Anything But Typical

Execution context: this and call()

Page 40 © 2010 Razorfish. All rights reserved.

Page 41: Anything But Typical

Execution context: this and call()

Page 41 © 2010 Razorfish. All rights reserved.

Page 42: Anything But Typical

Execution context with instances

Page 42 © 2010 Razorfish. All rights reserved.

Same outcome

Page 43: Anything But Typical

Page 43 © 2010 Razorfish. All rights reserved.

Inheritance

Page 44: Anything But Typical

Working with inherited behavior

Page 44 © 2010 Razorfish. All rights reserved.

LimitedContainer

Container

Object

Page 45: Anything But Typical

Working with inherited behavior

Page 45 © 2010 Razorfish. All rights reserved.

LimitedContainer

Container

Object

Page 46: Anything But Typical

Working with inherited behavior

Page 46 © 2010 Razorfish. All rights reserved.

LimitedContainer

Container

Object

Page 47: Anything But Typical

Page 47 © 2010 Razorfish. All rights reserved.

JavaScript is the Frankensteinof the OOP world

Page 48: Anything But Typical

Borrowing from multiple prototypes

Page 48 © 2010 Razorfish. All rights reserved.

Page 49: Anything But Typical

Page 49 © 2010 Razorfish. All rights reserved.

One small problem

Page 50: Anything But Typical

The constructor problem

Page 50 © 2010 Razorfish. All rights reserved.

Page 51: Anything But Typical

The constructor problem

Page 51 © 2010 Razorfish. All rights reserved.

Page 52: Anything But Typical

Fixing the constructor problem

Page 52 © 2010 Razorfish. All rights reserved.

Page 53: Anything But Typical

Fixing the constructor problem

Page 53 © 2010 Razorfish. All rights reserved.

Page 54: Anything But Typical

Page 54 © 2010 Razorfish. All rights reserved.

prototype isn’t love at first sight

Page 55: Anything But Typical

Page 55 © 2010 Razorfish. All rights reserved.

Putting prototypes to work

Page 56: Anything But Typical

Requirements

• Four kinds of rules on the form:

Required

Email

Number

Zip Code

• Multiple rules can apply to one field

• Rules need to be inferred from data-rich, standard HTML

• Error messages should be specific

• Flexible enough to be:

Usable for all other forms on the site

Extended for use in other projects

Page 56 © 2010 Razorfish. All rights reserved.

Page 57: Anything But Typical

The Plan

• On load:

1. Create validation rules for each field

2. Set up submit handler

• When form is submitted:

1. Cycle through all rules

2. If a field fails a rule

• Mark it invalid

• Create an error message for it

• Don’t check any more rules for that field

3. Show all error messages

Page 57 © 2010 Razorfish. All rights reserved.

Page 58: Anything But Typical

Page 58 © 2010 Razorfish. All rights reserved.

ValidatingForm

Object

Validator

NumberValidator

PatternValidator

RequiredValidator

EmailValidator

Page 59: Anything But Typical

Page 59 © 2010 Razorfish. All rights reserved.

Libraries are there to make life easier

Page 60: Anything But Typical

Page 60 © 2010 Razorfish. All rights reserved.

Think about your approach

Page 61: Anything But Typical

Page 61 © 2010 Razorfish. All rights reserved.

Dive in!

Page 62: Anything But Typical

Thank YouDan [email protected]

Blog: http://heydanno.com/blogCode demo: http://heydanno.com/sxsw

Page 62 © 2010 Razorfish. All rights reserved.