scope or not?

12

Click here to load reader

Upload: eric-brechemier

Post on 04-Jul-2015

1.009 views

Category:

Technology


0 download

DESCRIPTION

CC-BY-SA Eric Bréchemierhttps://github.com/eric-brechemier/scopeornot-presentationPresentation of the scopeornot library at ParisJS on December 21st, 2011.Quoting the README of the scopeornot library:"This project defines a single function scope() with multiple implementations. The goal is to make definition of modules and private scopes slightly simpler than using JavaScript Module pattern, and flexible enough to switch from static to dynamic module loading. You can choose one of the provided scope() implementations, replace it with another, tweak it to your needs, or create your own."scopeornot on GitHub:https://github.com/eric-brechemier/scopeornot

TRANSCRIPT

Page 1: scope or not?

Eric BréchemierEric Bréchemier

@eric_brechemier@eric_brechemier

github.com/eric-brechemiergithub.com/eric-brechemier

scope or not?

ParisJS, December 21ParisJS, December 21stst 2011 2011

Page 2: scope or not?

JavaScript Module Pattern

Page 3: scope or not?

(function(){

// a module

}());

Page 4: scope or not?

= Immediately-Invoked Function Expressions

Ben Almanhttp://benalman.com/news/2010/11/immediately-invoked-function-expression/

Page 5: scope or not?

(function(){

// private scope var id = "abc123";

function logout(){ location = "/logout?"+id; }

}());

Page 6: scope or not?

/*global APP:true, window */var APP = APP || {};

APP.session = APP.session ||(function(window,user){

// Public API return { logout: function(){ window.location = "/logout?"+user.id(); } };

}(window,APP.user));

Page 7: scope or not?

github.com/eric-brechemier/scopeornot

Page 8: scope or not?

scope(function(){

// a module

});

Page 9: scope or not?

scope(function(context){

// private scope var id = "abc123";

function logout(){ location = "/logout?"+id; }

});

Page 10: scope or not?

/*global scope */scope(function(context){

// Declare aliases var window = context.window, user = context["APP.user"];

// Public API return { logout: function(){ window.location = "/logout?"+user.id(); } };

}, ["window","APP.user"], "APP.session");

Page 11: scope or not?

2011-12-21 11 /

THANK YOU!

Page 12: scope or not?

2011-12-21 12 /

Credits & Further Reading

Slide TemplateAdapted from "tokyo-midtown-1.otp"

CC-BY-SA Chih-Hao Tsai

JavaScript Module Pattern: In-DepthJavaScript Scoping and Hoisting

by Ben CheeryUnnecessarily comprehensive look into a rather

insignificant issue of global objects creation by kangax

https://github.com/eric-brechemier/scopeornot-presentation