Here are the key points about closures:- A closure is a function that remembers values in enclosing scopes even if they are not present in memory. - In JavaScript, each function is a closure - it remembers its lexical scope through the scope chain.- Closures are created every time a function is created, at function creation time. - This allows a function defined inside another function to access variables from the parent function's scope.- The parent function and its variables effectively become part of the closed over function's scope.- Closures are useful for private variables, modules, and objects with methods.- In languages like JavaScript, closures solve the problem of referencing
Similar to Here are the key points about closures:- A closure is a function that remembers values in enclosing scopes even if they are not present in memory. - In JavaScript, each function is a closure - it remembers its lexical scope through the scope chain.- Closures are created every time a function is created, at function creation time. - This allows a function defined inside another function to access variables from the parent function's scope.- The parent function and its variables effectively become part of the closed over function's scope.- Closures are useful for private variables, modules, and objects with methods.- In languages like JavaScript, closures solve the problem of referencing
Similar to Here are the key points about closures:- A closure is a function that remembers values in enclosing scopes even if they are not present in memory. - In JavaScript, each function is a closure - it remembers its lexical scope through the scope chain.- Closures are created every time a function is created, at function creation time. - This allows a function defined inside another function to access variables from the parent function's scope.- The parent function and its variables effectively become part of the closed over function's scope.- Closures are useful for private variables, modules, and objects with methods.- In languages like JavaScript, closures solve the problem of referencing (20)
Here are the key points about closures:- A closure is a function that remembers values in enclosing scopes even if they are not present in memory. - In JavaScript, each function is a closure - it remembers its lexical scope through the scope chain.- Closures are created every time a function is created, at function creation time. - This allows a function defined inside another function to access variables from the parent function's scope.- The parent function and its variables effectively become part of the closed over function's scope.- Closures are useful for private variables, modules, and objects with methods.- In languages like JavaScript, closures solve the problem of referencing
3. Benefits
•No compile, no build. Just Code and Test
•Very, very short design cycles
•Less code
•Absolute Flexibility.
让你的工作变得没那么枯燥,减少“体力活”
生成代码,瞬间产生大量代码,改善你的KPI数据....
扩展视野,给出更好的解决方案
Python Py2Exe
Script to Executable
Ruby Exerb
Compiler/Embeder
PHP Bambalam
Java GCJ
...
15. 3.Prototype-based Inheritance
http://hi.baidu.com/aztack/blog/item/55ad0946b59268066a63e567.html
GrandChild Parent GrandParent
__proto__ __proto__ __proto__
null
name="Jack" function F1()
name="unnamed"
function F2()
function F4()
????????? ???????? function F3()
Linked List Prototype-based inheritance
share properties & methods
while classic inheritance only
More layer of indirection to implement share methods
Prototype-based Inheritance
17. 4.Scope
Function Declaration Function Expression
function f1(){...} function(){}
f1 function f1(){...} function (){...}
18. 4.Scope - JavaScript has only function scope,nothing else
Entering Executing Context
Code Execution
Hoisting:
函数、变量声明提升到最开始
19. 5.Closure
all parent scopes
- A closure is a combination of a function and statically saved
Stack-Oriented Programming Language
At return from a function,it's local variables are removed from stack
ESP
ESP
1.Not set to zero
2.Accessible in c/c++!
3.Results Undefined
behavior
20. 5.Closure
all parent scopes
- A closure is a combination of a function and statically saved
JavaScript anonymous Heap
VO
Variable Object data
local __parent__
variable
__parent__ Scope-Chain!!!
Function Object
Code
function(){
f [[Call]] this code with f() print(data.str);
}
goto page9
21. 5.Closure
all parent scopes
- A closure is a combination of a function and statically saved
def entryExit(f):
def new_f():
@entryExit
print "Entering", new_f.__name__
def someFunc:pass
f()
print "Exited", new_f.__name__
new_f.__name__ = f.__name__
return new_f
function entryExit(f){
function new_f(){
print("Entring",new_f.__name__);
f();
print("Exited",new_f.__name__);
} var f = entryExit(
new_f.__name__ = f.name; function(){...}
return new_f; );
}
22. Save function address and
function name
make a callable object with
operator()
consider number of
parameters and return type
Boost preprocessor
and
Variadic Templates to help
23. 5.Closure
all parent scopes
- A closure is a combination of a function and statically saved
A common mistake : Creating closures in loops
function setupHelp() {
var helpText = [
{'id': 'email', 'help': 'Your e-mail address'},
{'id': 'name', 'help': 'Your full name'},
{'id': 'age', 'help': 'Your age (you must be over 16)'}
];
for (var i = 0; i < helpText.length; i++) {
var item = helpText[i];
document.getElementById(item.id).onfocus = function() {
showHelp(item.help);
}
}
}
https://developer.mozilla.org/en/JavaScript/Guide/Closures
24. 5.Closure
all parent scopes
- A closure is a combination of a function and statically saved
Fix: Avoid creating unneccessary stuffs in loops
function setupHelp() {
var helpText = {
'email': 'Your e-mail address',
'name': 'Your full name',
'help': 'Your age (you must be over 16)'
};
function ShowHelp(){
alert(helpText[this.id]);
}
for (var id in helpText) {
document.getElementById(id).onfocus = showHelp
}
}
https://developer.mozilla.org/en/JavaScript/Guide/Closures
31. Resources - people,books,links
Dmitry A. Soshnikov
http://dmitrysoshnikov.com
developer.mozilla.org
John Resig @mozilla
http://ejohn.org/
JavaScript Secrets Pro JavaScript
32. Resources - people,books,links
•ECMA-262-5th
•O'Reilly - Javascript - The Definitive Guide, 4th Edition
•Oreilly.High.Performance.JavaScript.Mar.2010
•Pro.JavaScript.Design.Patterns
•Professional.JavaScript.for.Web.Developers.2nd.Edition
Nicholas C. Zakas @yahoo
http://www.nczonline.net/
33. Resources - people,books,links
http://www.crockford.com
YUI blog
JavaScript
The Theory of the Dom
Advanced JavaScript
Browser Wars
Quality
JavaScript: The Good Stuff
The State of Ajax
The JSON Saga
Ajax Performance
The State and Future of JavaScript
Douglas Crockford @yahoo