Pregunta

I have a simple class

function TrueNinja() {
   this.vanish = function() { return this; };
}

creating a new object from this

var someNinja = new TrueNinja();

when i do the following in chrome i get two different outputs

console.log(someNinja instanceof TrueNinja); // i get true
console.log("someNinja: " + someNinja instanceof TrueNinja); //i get false

WHY?? :-( the first statement is the correct output since someNinja IS an instance of TrueNinja... but why do i get false in the next statement?

¿Fue útil?

Solución 2

You need to give like this:

"someNinja: " + (someNinja instanceof TrueNinja);

The reason is, the whole "someNinja: " + someNinja is compared with TrueNinja.

So, this way, it compares, "someNinja: " + someNinja With the TrueNinja, where, the former is string and latter is TrueNinja. Hope it is clear! :)

The actual reason is because of Operator Precedence, where + comes before instanceof.

Otros consejos

That's due to operator precedence, + is evaluated before instanceof.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top