Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of "in" in JavaScript

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.

like image 952
Bunkai.Satori Avatar asked Sep 23 '12 02:09

Bunkai.Satori


People also ask

What is the use of in in JavaScript?

The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists. Anatomy of a simple JavaScript object.

What does 3 dots mean in JavaScript?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed.

What does != Mean in JavaScript?

The inequality operator ( != ) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.

What is difference between for in and for OF in JavaScript?

The only difference between them is the entities they iterate over: for..in iterates over all enumerable property keys of an object. for..of iterates over the values of an iterable object.


2 Answers

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;
};
like image 89
Mike Samuel Avatar answered Oct 26 '22 09:10

Mike Samuel


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

like image 28
Elias Van Ootegem Avatar answered Oct 26 '22 10:10

Elias Van Ootegem