- 浏览: 401037 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
The with() {} statement is meant to provide convenience.
and there are some golden laws about the with() {} statement.
- it only use/assign existing properties
- it does not affect what is 'this'
- the scoped variable inside with statement take precedence over others with the same name
- it slows things down.,
let's first see some simple example that shows something about the with statement.
/************************************** *@Summary * this shows the simple use of with statement * * the key rule about the use of the statement is that it * 1. does not affect what is 'this' context. * 2. it does not help create properties that is not in the 'with' scoped variable. You can only use and assign existing properties in the with() {} statement * 3. within the scope of the statement, the properties introduced by with(){} take precedence over the other variables. * 4. and it slows things down (not sure where comes the extra overhead) * @Usage: * * @TODO: * test it ***************************************/ var use = "other"; var katana = { isSharp: true, use: function () { this.isSharp = !this.isSharp; } }; //with (katana) { // assert(true, "you can still call outside mehtods."); // isSharp = false; // use(); // // this illustrate the fact that local with variable take precedence // assert(use !== "other", "Use is a function, from the kantana object"); // assert(this !== katana, "This isn't changed - it keeps its original value"); //} // commentted out, to test it , copy the following code to the test html and do the test //assert(typeof isSharp === "undefined", "Outside the with, the properties don't exist."); //assert(katana.isSharp, "Verify that the method was used correctly in the with.");
and below is its test cases.
<!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="simplewith.js" type="text/javascript"></script> <script type="text/javascript"> window.onload = function () { test("with(){} doest not change what 'this' is. and it simplifies things", function () { with (katana) { assert(true, "you can still call outside mehtods."); isSharp = false; use(); // this illustrate the fact that local with variable take precedence assert(use !== "other", "Use is a function, from the kantana object"); assert(this !== katana, "This isn't changed - it keeps its original value"); } assert(typeof issharp === "undefined", "outside the with, the properties don't exist."); assert(katana.isSharp, "verify that the method was used correctly in the with."); }); test("you cannot access non-existing properties inside with(){} statement", function () { with (katana) { isSharp = false; cut = function () { isSharp = false; }; } assert(!katana.cut, "The new cut property wasn't introduced here."); assert(this.cut, "It was made as a member variable to the anonymous function that contains the test instead."); }); }; </script> <style type="text/css"> #results li.pass {color: Green} #results li.fail { color: Red} </style> </head> <body> <ul id="results" </body> </html>
also, with the 'with' statement, you can chang the way how the code is written to simulate some object-oriented javascript code.
/************************************** *@Summary * this shows how you can use the with statement to facilitate writting some OO like code. * * the key rule about why the use of the statement has the advantage is that * 1.it introduce a local scope * 2. you can have something like private and public, which is missing in non-with statement * 3. you can access the private and public through names alike inside the with statement, but outside the visibility is different. * @Usage: * * @TODO: * test it ***************************************/ function Ninja(){with(this){ // Private Information var cloaked = false; // Public property this.swings = 0; // Private Method function addSwing(){ return ++swings; } // Public Methods this.swingSword = function(){ cloak( false ); return addSwing(); }; this.cloak = function (value) { return value != null ? cloaked = value : cloaked; }; } } // below is the test code. var ninja = new Ninja(); assert(!ninja.cloak(), "We start out uncloaked."); assert(ninja.swings == 0, "And with no sword swings."); assert(ninja.cloak(true), "Verify that the cloaking worked correctly."); assert(ninja.swingSword() == 1, "Swing the sword, once."); assert(!ninja.cloak(), "The ninja became uncloaked with the sword swing.");
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 929the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2255Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1670In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 908In 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 974Event 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 1032We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 822For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 885Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 769There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 768All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 524though 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 537IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1043there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1044expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1149it is not as simple as you thin ...
相关推荐
"JavaScript in 10 Minutes" is a concise guide that aims to provide intermediate to advanced JavaScript developers with an overview of the language's unique features and nuances. This document is not ...
The function Statement Versus the function Expression Section B.10. Typed Wrappers Section B.11. new Section B.12. void Appendix C. JSLint Section C.1. Undefined Variables and Functions Section...
Each recipe includes a concise statement of the problem and the approach you should take in order to solve it. A full code solution is also given, along with an in-depth explanation, so you can build ...
8. **Nashorn JavaScript Engine:** JDK 1.8 includes a built-in JavaScript engine called Nashorn, allowing Java developers to execute JavaScript code within the JVM and facilitate interoperation between...
Each recipe includes a concise statement of the problem and the approach you should take in order to solve it. A full code solution is also given, along with an in-depth explanation, so you can build ...
box for window.alert, window.confirm, and window.prompt statements in the JavaScript. The rest of the JavaScript will run. 3. When using a Web Add-in Extensibility-based Add-in, if the JavaScript ...
AJAX (Asynchronous JavaScript and XML) is a technique used for creating faster and more interactive web applications by exchanging data with a server behind the scenes and updating parts of a web ...
The ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement; see Closing Connections in Processing SQL Statements. RowSet 1.1: The ...
然而,某些技术如CORS(跨源资源共享)或JSONP(JSON with Padding)可以帮助绕过这一限制,使书签能够与银行网站交互。 6. **存储与同步**:一旦数据被提取和处理,可能需要将其存储在浏览器的本地存储(如...
PEP 343: The ‘with’ statement Writing Context Managers The contextlib module PEP 366: Explicit Relative Imports From a Main Module PEP 370: Per-user site-packages Directory PEP 371: The ...
The goto Statement 106 Branching 107 Looping 115 Summary 127 Exercises 127 Chapter 5: More About Variables 130 Type Conversion 130 Complex Variable Types 140 String ...
Fixed focus issue with the suggestion list that appears following "in" in a C# foreach statement. (case=73981) 10226 Surround With feature is no longer invokable within comments, string literals, and ...
- **Scripting Language Support:** Improved integration with scripting languages like JavaScript, Ruby, and Python through the javax.script package. - **JSON Processing (JSR 353):** A standard API ...
The directory from the --with-config-file-path compile time option, or the ; Windows directory (C:\windows or C:\winnt) ; See the PHP docs for more specific information. ; ...
76. Web search with JavaScript 162 77. 如何防止他人使用旧id和旧口令访问Domino服务器 164 78. Fixing the Domino CheckBox Bug 165 79. Managing JavaScript "popup" windows 172 80. Quick, easy, foolproof ...
76. Web search with JavaScript 162 77. 如何防止他人使用旧id和旧口令访问Domino服务器 164 78. Fixing the Domino CheckBox Bug 165 79. Managing JavaScript "popup" windows 172 80. Quick, easy, ...
12.9 The Object Model of JavaScript 524 12.10 Implementation of Object-Oriented Constructs 527 Summary • Review Questions • Problem Set •Programming Exercises 530 Chapter 13 Concurrency ...
例如:“In Java, you define a class with the `class` keyword, like `public class Car { ... }`”。 4. **Database(数据库)** - 数据库是用来组织、存储和检索数据的系统。例如:“MySQL is a popular ...
Web search with JavaScript - **搜索方法**: 展示如何使用JavaScript实现Web搜索功能。 #### 77. 如何防止他人使用旧id和旧口令访问Domino服务器 - **防止方法**: 通过定期更改密码策略和禁用旧ID来提高安全性。 ...
- **Reduce memory leaks dramatically with the “using” statement (利用“using”语句大幅减少内存泄漏)** - .NET中的`using`语句提供了一种优雅地管理资源的方式。它确保即使在发生异常的情况下,资源也能正确...