- 浏览: 401114 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
Javascript has four kind of functions, namely
- global function
- constructor
- prototype
- closure
I may be wrong about the types, but the rule of thumb is that the functions differs from each other in terms of how the 'this' pointer is determined.
another things that you might be aware is that , with the .call() and .apply() method, you can manipulate the context of a function.
let's take an example.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="../unit.js" type="text/javascript"></script> </head> <style type="text/css" > #results li.pass { color: Green } #results li.fail { color: Red } </style> <body> <button id="test">click me!</button> <script type="text/javascript"> function trigger(elem, name) { elem[name](); } window.onload = function () { test("function context ", function () { var Button = { click: function () { this.clicked = true; } }; var elem = document.getElementById("test"); elem.addEventListener("click", Button.click, false); trigger(elem, "click"); assert(elem.clicked, " The clicked property was accidently set on the element"); elem.removeEventListener("click", Button.click, false); }); }; </script> <ul id="results" /> </body> </html>
please be NOTED that the unit.js is a home-crafted test framework, which I will share in some other post.
as you can see, if you run this test, when you click on the button, the button with id test will have its clicked proeprty set, which is not what we want, it happens because the 'this' inside of the click function is referring to whatever context the function currently has.
a revised way is like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="../unit.js" type="text/javascript"></script> <script src="functioncontext.js" type="text/javascript"></script> </head> <style type="text/css" > #results li.pass { color: Green } #results li.fail { color: Red } </style> <body> <button id="test">click me!</button> <script type="text/javascript"> function trigger(elem, name) { elem[name](); } window.onload = function () { test("function context 2", function () { var Button = { click: function () { this.clicked = true; } }; var elem = document.getElementById("test"); var eventhandler = bind(Button, "click"); elem.addEventListener("click", eventhandler, false); trigger(elem, "click"); assert(Button.clicked, "the clicked property was set on our object"); elem.removeEventListener("click", eventhandler, false); }); }; </script> <ul id="results" /> </body> </html>
the content of the "functioncontext.js" is like this
function bind(context, name) { return function () { return context[name].apply(context, arguments); } }
the key here is that we used the .call() funtion, which we can override the function context (affect what this binds to).
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 930the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2256Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1671In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 909In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 706Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 975Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2083In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1033We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 823For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 886Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 770There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 769All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 525though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 550when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 521When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 749while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 538IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1044there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1045expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1150it is not as simple as you thin ...
相关推荐
secrets_of_javascript_closures.pdf
Java Closures and Lambda introduces you to significant new changes to the Java language coming out of what is termed Project Lambda. These new changes make their debut in Java 8, and their highlight ...
### Apress - Java Closures and Lambda (2015):关键知识点解析 #### 引言 本书《Apress - Java Closures and Lambda》聚焦于Java 8中的新特性——闭包(Closures)和Lambda表达式。这些新功能不仅为Java语言带来...
JavaScript,作为一种广泛应用于Web开发的脚本语言,其高级特性如闭包(closures)、原型(prototype)和继承(inheritance)是理解其精髓的关键。本文将深入探讨这些概念,帮助开发者更好地掌握JavaScript的核心。 ...
The first module Mastering JavaScript, stress on practical aspects of Javascript development like—Functions and Closures, Runtime debugging techniques, project layout, events and DOM processing, ...
- Get familiar with the Functions and Closures of JavaScript - Explore Regular Expressions in JavaScript - Code using the powerful object-oriented feature in JavaScript - Test and debug your code ...
, AMD, Asynchronous Operations, Callbacks, Promises and Deferreds, Code Quality, Function Polymorphism, Function Scope, Hoisting and Closures, Functional Programming and Stateless Functions, ...
深入理解JavaScript系列(11):执行上下文(Execution Contexts) 深入理解JavaScript系列(12):变量对象(Variable Object) 深入理解JavaScript系列(13):This? Yes, this! 深入理解JavaScript系列(14)...
深入理解JavaScript系列(11):执行上下文(Execution Contexts) 深入理解JavaScript系列(12):变量对象(Variable Object) 深入理解JavaScript系列(13):This Yes this 深入理解JavaScript系列(14):...
If you want to write ... If you’re familiar with concepts such as closures and prototypal inheritance, you’ll be able to determine why some patterns may be more suitable for your projects than others.
It documents every class, object, constructor, method, function, property, and constant defined by JavaScript 1.5 and ECMAScript version 3. Part IV is a reference for client-side JavaScript, ...
Get familiar with the Functions and Closures of JavaScript Explore Regular Expressions in JavaScript Code using the powerful object-oriented feature in JavaScript Test and debug your code using ...
Get a run through of the basic language constructs, Functions, and Closures of JavaScript Code using the powerful object-oriented feature in JavaScript Master DOM manipulation, cross-browser ...
Chapter 2: Functions, Closures, And Modules Chapter 3: Data Structures And Manipulation Chapter 4: Object-Oriented Javascript Chapter 5: Javascript Patterns Chapter 6: Testing And Debugging Chapter 7:...
Variables: Scopes, Environments, and Closures Chapter 17. Objects and Inheritance Chapter 18. Arrays Chapter 19. Regular Expressions Chapter 20. Dates Chapter 21. Math Chapter 22. JSON Chapter 23. ...
It documents every class, object, constructor, method, function, property, and constant defined by JavaScript 1.5 and ECMAScript version 3. Part IV is a reference for client-side JavaScript, ...