2

I've recently been given this piece of javascript (as part of a jQuery function)...

var h = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var w = "innerWidth" in window ? window.innerWidth : document.documentElement.offsetWidth;

I've never seen "innerHeight" in used before, and I cannot find any explanation of what it is for, or why it's used. (Doing a Google search for the word in isn't exactly helpful.)

Is it jQuery syntax?

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • I've put "javascript in" in google and it provided a bunch of relevant results. Haven't you heard of http://google.com? – zerkms Apr 18 '14 at 09:43
  • @KarlenKishmiryan ,plese , OP gets nervous when he sees tag POSSIBLE DUPLICATE OF ............. .He didnt do any crime :( – Pratik Joshi Apr 18 '14 at 09:44
  • @Pratik Joshi: they did actually. They didn't bother trying to solve it themselves. – zerkms Apr 18 '14 at 09:45
  • @Amit Joki: excuse me? No it's not. Why are you asking? – zerkms Apr 18 '14 at 09:45
  • @zerkms, that was a joke ;) – Amit Joki Apr 18 '14 at 09:46
  • @zerkms, let me see - erm, yes, I've heard of Google - I even mention it in my question. However, I stupidly didn't search for `javascript in`... I searched for `javascript var in` which gave me masses of results about `for(var x in y)` – freefaller Apr 18 '14 at 09:46
  • @zerkms ,Yes you are excused ,You have given ExCuSe. – Pratik Joshi Apr 18 '14 at 09:46
  • @zerkms, in reponse to `They didn't bother trying to solve it themselves`... are you always so objectionable? Yes, I did try and solve it for myself - I searched Google (which you obviously missed me mentioning in my question) but obviously badly. There is no need to be obnoxious about it - or is that a privilege of having nearly 100k rep?? – freefaller Apr 18 '14 at 09:54
  • @freefaller: the thing is - if a person asks about trivial syntax - in 99.99% it means they didn't even try to google. My apologies to you and I honestly cannot understand how it's possible to not google about it in a minute. It seriously surprises me. – zerkms Apr 18 '14 at 10:01
  • @zerkms - I'm actually known by my colleagues for being really bad at coming up with good Google search terms, this appear to be no different. My mistake was thinking it was part of the `var` declaration, rather than an operator in it's own right. (Apology accepted) – freefaller Apr 18 '14 at 10:07

4 Answers4

5

No, the in operator is core JavaScript.

It resolves as true if the object on the right hand side has a property with a name that matches the string on the left hand side.

See a simple example:

var foo = {
    a: 1,
    b: undefined
}

var o = document.getElementById('output');

o.innerHTML = ("a" in foo) + "<br>" + 
    ("b" in foo) + "<br>" + 
    ("c" in foo) + "<br>";
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks Quentin - as zerkms "helpfully" points out in their comment under the question, I didn't search for the correct thing - your explanation is excellent – freefaller Apr 18 '14 at 09:49
2

This is pure JS.

This is similar to object.property with a little difference.

object.property will fail in cases property is not set and also in cases it has been set to some false value, e.g. undefined, null, 0 etc

property in object is slightly better because it doesn't looks for its value.

Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56
2

Check if a object's property exists.

in is used to check if an object contains a specific property , this is rappresented by a string.

var obj={a:1,b:2}

// in
console.log('a' in obj); // true
console.log('c' in obj); // false

Said that.. there is a faster/shorter way to do that.

// this does not work if the property's value is 0,null,undefined,false...
console.log(!!obj.a); // true  
console.log(!!obj.c); // false

demo

http://jsfiddle.net/cLDEd/

in your case a better solution would be:

var H=window.innerHeight?window.innerHeight:document.documentElement.offsetHeight

or even better :

var H=window.innerHeight||document.documentElement.offsetHeight

Theoretically window.innerHeight could be 0 , so at that point it would try to get the document.documentElement.offsetHeight whitch returns undefined and you should add another or ||0 to return 0 and not undefined. But that does not happen ;).

Putting window.innerHeight in front of document.documentElement.offsetHeight accelerates the check as window.innerHeight is standard, so there is a higher chance that it's the correct choice.

cocco
  • 16,442
  • 7
  • 62
  • 77
0

You can see it as if innerHeight <inside> window then window....

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
to4dy
  • 128
  • 1
  • 1
  • 10