JavaScript中with/this用法
2013-10-21 09:27
615 查看
1.with用法
为一个或一组语句指定默认对象。不过,with用法并不推荐!用法:with (<对象>) <语句>;
with 语句通常用来缩短特定情形下必须写的代码量。在下面的例子中,请注意 Math 的重复使用:
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10);
y = Math.tan(14 * Math.E);
当使用 with 语句时,代码变得更短且更易读:
with (Math) {
x = cos(3 * PI) + sin(LN10);
y = tan(14 * E);
}
HISTORY
EDIT
TABLE OF CONTENTS
Summary
Syntax
Parameters
Description
Examples
Example: Using
with
TAGS
FILES
Use of the
withstatement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the "Ambiguity Con" paragraph
in the "Description" section below for details.
Summary
Extends the scope chain for a statement.Statement | |
Implemented in | JavaScript 1.0 |
ECMAScript Edition | ECMA-262 |
Syntax
with (object) { statement }
Parameters
objectAdds the given obj
4000
ect to the scope chain used when evaluating the statement. The parentheses around object are required.
statementAny statement. To execute multiple statements, use a block statement
({ ... }) to group those statements.
Description
JavaScript looks up an unqualified name by searching a scope chain associated with the execution context of the script or function containing that unqualified name. The 'with' statement adds the given object to the head of this scope chain during the evaluationof its statement body. If an unqualified name used in the body matches a property in the scope chain, then the name is bound to the property and the object containing the property. Otherwise a 'ReferenceError' is thrown.
Using
withis not recommended, and is forbidden in ECMAScript 5 strict
mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.
(On a historical note, Firefox 1.5 used to generate a warning when the 'with' statement was used: "deprecated with statement usage". This has been removed in Firefox 1.5.0.1 (bug 322430).)
Performance Pro & Con
Pro: 'with' can help reduce file size by reducing the need to repeat a lengthy object reference without performance penalty. The scope chain change required by 'with' is not computationally expensive. Use of 'with' will relieve
the interpreter of parsing repeated object references. Note, however, that in many cases this benefit can be achieved by using a temporary variable to store a reference to the desired object.
Con: 'with' forces the specified object to be searched first for all name lookups. Therefore all identifiers that aren't members of the specified object will be found more slowly in a 'with' block. Where performance is important,
'with' should only be used to encompass code blocks that access members of the specified object.
Ambiguity Con
Con: 'with' makes it hard for a human reader or JavaScript compiler to decide whether an unqualified name will be found along the scope chain, and if so, in which object. So given this example:
function f(x, o) { with (o) print(x); }
only when
fis called is
xeither found or not, and if found, either in
oor
(if no such property exists) in
f's activation object, where
xnames the first
formal argument. If you forget to define
xin the object you pass as the second argument, or if there's some similar bug or confusion, you won't get an error -- just
unexpected results.
Con: Code using
withmay not be forward compatible, especially when used with something else than a plain object. Consider this example:
[code]console[code]o[code]
相关文章推荐
- JavaScript中with、this用法小结
- javaScript 中的 with 和 this 的用法
- JavaScript中with、this用法小结
- JavaScript中with、this用法小结
- JavaScript中的this、with的用法
- JavaScript中的this、with的用法
- JavaScript中with、this用法
- Javascript的this用法
- javascript面向对象之this关键词用法分析
- 理解并掌握 JavaScript 中 this 的用法
- Javascript 作用域与this的用法
- JavaScript中的this用法
- javascript this用法小结
- JavaScript this 的用法
- java中this用法和javascript中的this用法
- Javascript的This用法
- Javascript的this用法
- Javascript的this用法
- 一种简易的方式阐述JavaScript中this的用法
- 深入浅出分析javaScript中this用法