javascript - JavaScript 中 "in"的解释

标签 javascript web-services web-applications web

请考虑下面的代码示例,并将重点放在变量赋值上。由于我从未在 C++ 中见过这种形式,以下是什么意思:“上传”in new XMLHttpRequest`。

我需要很好地解释以下语句的含义:progress: "upload"in new XMLHttpRequest。特别是 in 在 C++ 中不存在。 in 应该做什么?

tests = {
  filereader: typeof FileReader != 'undefined',
  dnd: 'draggable' in document.createElement('span'),
  formdata: !!window.FormData,
  progress: "upload" in new XMLHttpRequest
};

谢谢。

最佳答案

Chapter 11.8.7 - The in operator

Return the result of calling the [[HasProperty]] internal method of rval with argument ToString(lval).

这意味着

(lval in rval)

rval 是一个对象并且它有一个名为 String(lval) 的属性时为真。

in 也用在 for (... in ...) 循环中,但这只是类似的语法,而不是使用此运算符。


"upload" in new XMLHttpRequest

这是在询问“XMLHttpRequest 实例是否具有名为‘upload’的属性?”它正在有效地检查此浏览器是否具有可能并非在所有浏览器上都存在的特定功能。

upload 特别在 XMLHttpRequest Level 2 中指定作为 supports certain event handler 的对象让您监控上传进度:

interface XMLHttpRequestEventTarget : EventTarget {
  // event handlers
  [TreatNonCallableAsNull] attribute Function? onloadstart;
  [TreatNonCallableAsNull] attribute Function? onprogress;
  [TreatNonCallableAsNull] attribute Function? onabort;
  [TreatNonCallableAsNull] attribute Function? onerror;
  [TreatNonCallableAsNull] attribute Function? onload;
  [TreatNonCallableAsNull] attribute Function? ontimeout;
  [TreatNonCallableAsNull] attribute Function? onloadend;
};

关于javascript - JavaScript 中 "in"的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12549352/

相关文章:

.net - 由于身份验证失败,无法满足对安全 token 的请求;一种安装有效,另一种则无效

node.js - 带有 Express js 的 Angular 2 cli

javascript - 使用这个系统来布局网页有什么潜在的问题?

javascript - 使用 jQuery/JavaScript 获取表单对选择框更改的操作

javascript - 从函数本身内部的按钮调用 jquery 函数

javascript - 如何在 Bootstrap 中制作包含 3 个以上图像的轮播?

java - 如何构建两个 Wicket 应用程序

Javascript-将每个传入数据直接保存到磁盘

java - 如何在 Java 中手动将 cookie 添加到 Web 服务调用中?

php - 比 MVC 更适合 Web 应用程序的架构?