11
Structuring JavaScript Code Vedran Maršić

Structuring JavaScript Code

  • Upload
    axilis

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Structuring JavaScript Code

Structuring JavaScript CodeVedran Maršić

Page 2: Structuring JavaScript Code

Who’s talkin to you Vedran Maršić AKA Fosna

dev at http://axilis.com/

organizing programming workshops for kids Logo, Scratch, HTML, CSS, JavaScript, Python

@Fosna_

[email protected]

Page 3: Structuring JavaScript Code

Some of JavaScript Object Design Patterns

Prototype Pattern (Revealing) Module Pattern Revealing Prototype Pattern

Page 4: Structuring JavaScript Code

Prototype Pattern

PROS

• Native to JavaScript• Functions loaded to

memory once• Easy inheritance

CONS

• "this" becomes tricky• No encapsulation• Constructor is separate

from prototype definition

Page 5: Structuring JavaScript Code

Revealing Module Pattern

PROS

• Encapsulation • public and private

members

• No need to use “this“

CONS

• Copies functions• Inheritance is difficult• Private methods and

functions lose extendibility since they are inaccessible 

Page 6: Structuring JavaScript Code

Revealing Prototype Pattern

PROS

• Private static methods. • Should pass reference to

object instance to make like instance methods.

• Functions loaded to memory once• Easy inheritance

CONS

• "this" becomes tricky - you have to pass it arround• No variable encapsulation• Constructor is separate

from prototype definition

Page 7: Structuring JavaScript Code

Conclusion

Revealing Module Pattern for building applications

Revealing Prototype Pattern for building frameworks

Page 8: Structuring JavaScript Code

Things to remember

Immediately-Invoked Function Expression (IIFE) Namespacing Pesky “this“

Page 9: Structuring JavaScript Code

Resources

http://weblogs.asp.net/dwahlin/techniques-strategies-and-patterns-for-structuring-javascript-code

http://www.pluralsight.com/courses/structuring-javascript http://blog.bigbinary.com/2011/08/18/understanding-bind-and-

bindall-in-backbone.html

Page 10: Structuring JavaScript Code

Demo Code

https://github.com/axilis/StructuringJavascriptCode.git

Page 11: Structuring JavaScript Code

Questions please!Thx for listening