0

So I am getting incredible weird java script behavior inside a chrome tab. The page is behind a login so I can't post it but can someone explain exactly what is happening???

for(var z in ""){ console.log(z) }
contains
//undefined

hmm...

var key = ""

for(var i in key){ console.log(i) }
contains
//undefined
Object.getOwnPropertyNames(key)
//["length"]
Object.getOwnPropertySymbols(key)
//[]
window[key]
//undefined

At first I thought this was one of those JS behaviors and was ready to submit it to JSWTF but the behavior runs properly in another chrome tab:

for(var i in ""){ console.log('ran',i) }
//undefined

How did a value get assigned to a blank string? Where is it? What is the for loop doing?

edit: The same page in firefox returns expected behavior in console. I have not tested other browsers

lonewarrior556
  • 3,917
  • 2
  • 26
  • 55
  • 1
    You mention Chrome specifically - does that mean the same page doesn't behave that way in other browsers? – nnnnnn Apr 13 '16 at 19:12
  • 1
    closely related: [How to define method in javascript on `Array.prototype` and `Object.prototype` so that it doesn't appear in `for in` loop](http://stackoverflow.com/q/13296340/1048572) - something in that particular page added an enumerable `contains` property to `String.prototype`. – Bergi Apr 13 '16 at 19:18
  • 1
    this means the string prototype has been modified. Try opening a console here on stack overflow and you will get a lot more weird methods :) – Damon Apr 13 '16 at 19:19

1 Answers1

1

You have an ES6 shim on the original page which adds the function contains() to the String prototype. You can do this yourself by doing something like:

String.prototype.contains = 
    function(e) {
        return this.indexOf(e) > -1;
    };

The ES6 function ultimately standardized on is includes(), so you'll probably see that function name change in the future when a developer updates the shim.

Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52