今天看面试题时,突然发现了一个javaScript中的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);
}
this 对象 返回“当前”对象。在不同的地方,this 代表不同的对象。如果在 JavaScript 的“主程序”中(不在任何 function 中,不在任何事件处理程序中)使用 this,它就代表 window 对象;如果在 with 语句块中使用 this,它就代表 with 所指定的对象;如果在事件处理程序中使用 this,它就代表发生事件的对象。
分享到:
相关推荐
This compact short book will help you learn how to use modern JavaScript to make games for web browsers. They’re effortless to use and they work everywhere. If you’ve ever wanted to make a game, ...
### JavaScript中的`eval`与`with`用法...- **不建议使用**:现代JavaScript编码实践中一般避免使用`eval`和`with`,除非有充分的理由。现代前端框架和库提供了更安全、高效的替代方案来处理动态代码生成和属性访问。
This completely updated second edition covers everything you need to know to get up-to-speed with JavaScript development and add dynamic enhancements to web pages, right from the basics. As well as ...
Beginning JavaScript with DOM Scripting and Ajax will take you from being a JavaScript novice to work freely with this important technology – begin your JavaScript journey today! What you'll learn ...
With this hands-on guide, author Kyran Dale teaches you how build a basic dataviz toolchain with best-of-breed Python and JavaScript libraries—including Scrapy, Matplotlib, Pandas, Flask, and D3—...
This book targets IoT enthusiasts and web developers who would like to build IoT-based applications with Raspberry Pi, Arduino and JavaScript. Some knowledge about electronics and familiarity with ...
With this hands-on guide, you’ll create a JavaScript-based data processing pipeline, handle common and exotic data, and master practical troubleshooting strategies. You’ll also build interactive ...
With this practical book, web developers and frontend software engineers will learn how to use native UI elements, platform APIs (such as geolocation and local storage), and platform-specific design ...
This book will present various JavaScript and p5.js features and concepts in the following chapters. The knowledge will be reinforced by building several useful examples like an animation and a data...
This two-volume book shows how to design and implement web applications with a model-based engineering approach, using the two most relevant technology plat- forms for web development: JavaScript and ...
JavaScript采用正则表达式实现startWith、endWith效果函数 代码如下:String.prototype.startWith=function(str){ var reg=new RegExp(“^”+str); return reg.test(this); } String.prototype.endWith=function...
在JavaScript中,通过避免使用`this`、全局变量和修改传入对象来实现纯函数。 2. **高阶函数**:这些是接受一个或多个函数作为参数,或者返回一个函数的函数。在JavaScript中,`Array.prototype.map()`、`filter()`...
Learn how to program JavaScript while creating interactive ... This book encompasses all of the basic features of JavaScript with aspects of the Web Audio API to heighten the capability of any browser.
This book will teach you how to develop JavaScript applications with simple to use, yet powerful JavaScript technologies and host everything in the cloud in an economic and robust way in AWS. ...
在JavaScript中,`String.prototype.startsWith()` 和 `String.prototype.endsWith()` 方法分别用于判断字符串是否以指定的子串开头或结尾。这两个方法在ES6中被标准化并引入到JavaScript语言中。然而,在某些旧版本...