最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

web services - Explanation of "in" in JavaScript - Stack Overflow

matteradmin9PV0评论

Please, consider the code sample below, and focus on variable assignments. Since I have never seen such form in C++, what does the following mean: "upload" in new XMLHttpRequest`.

I would need a good explanation of what does the following statement mean: progress: "upload" in new XMLHttpRequest. Especially, the in is not in C++ present. What is that in supposed to do?

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

thank you.

Please, consider the code sample below, and focus on variable assignments. Since I have never seen such form in C++, what does the following mean: "upload" in new XMLHttpRequest`.

I would need a good explanation of what does the following statement mean: progress: "upload" in new XMLHttpRequest. Especially, the in is not in C++ present. What is that in supposed to do?

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

thank you.

Share Improve this question edited Sep 23, 2012 at 3:15 Bunkai.Satori asked Sep 23, 2012 at 2:52 Bunkai.SatoriBunkai.Satori 4,75813 gold badges52 silver badges78 bronze badges 2
  • 4 developer.mozilla/en-US/docs/JavaScript/Reference/Operators/… – Dennis Commented Sep 23, 2012 at 2:56
  • @Dennis: +1, Hi Dennis. Complete answer is within that link. Thank you. – Bunkai.Satori Commented Sep 23, 2012 at 3:19
Add a ment  | 

5 Answers 5

Reset to default 6

Chapter 11.8.7 - The in operator says

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

which means that

(lval in rval)

is true when rval is an object and it has a property named String(lval).

in is also used in for (... in ...) loops but that is just similar syntax, not a use of this operator.


"upload" in new XMLHttpRequest

This is asking "does an XMLHttpRequest instance have a property named 'upload'?" It's effectively checking whether this browser has a particular feature which might not be present on all browsers.

upload in particular is specified in XMLHttpRequest Level 2 as an object that supports certain event handler to let you monitor the progress of an upload:

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;
};

The statement 'draggable' in document.createElement('span') returns a boolean. It checks to weather or not the element (in this case a span) has the draggable property, if it does true is returned, if it doesn't the return value will be false. There's nothing more to it.

If you see code like this:

for (varName in obj)
{
    //some stuff
}

All you really are seeing is a loop over the enumerable properties of the object (obj). Assume an object that looks like this:

var obj = {foo:'bar',non:'sense'};

Then varName will be equal to foo, then non etc... in other words: the "x in y statement"s main goal is to check the existence of enumerable (public) properties of an object

  • typeof FileReader != 'undefined'
    • true if an object named FileReader exists, false otherwise

  • 'draggable' in document.createElement('span')
    • true if <span> element allows the draggable property/method, false otherwise

  • !!window.FormData
    • true if window object has a property/method with the name FormData, false otherwise

  • "upload" in new XMLHttpRequest
    • true if XMLHttpRequest object has a property/method with the name upload, false otherwise

'x' in y will return true if "y" contains a property named "x"

By the way, you shouldn't expect Javascript and C++ to be so similar. They are quite different.

tests = {
  filereader: typeof FileReader != 'undefined',
  dnd: 'draggable' in document.createElement('span'),// Basically checks the draggable property of span
  formdata: !!window.FormData,
  progress: "upload" in new XMLHttpRequest// checking for the upload property of XMLHttpRequest/, you can add an eventlistner to check the progress event. 
};

As far as "in" goes it is nothing but checking if the element is present .It returns a boolean value

Post a comment

comment list (0)

  1. No comments so far