Created
January 21, 2012 15:18
-
-
Save gryzzly/1653043 to your computer and use it in GitHub Desktop.
instanceof vs Object.prototype.toString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function () { | |
// helper to set prototype chain up | |
var Constructor = function () {}; | |
Constructor.prototype = Array.prototype; | |
var NotRealArray = function () {}; | |
// inherit from Array | |
NotRealArray.prototype = new Constructor; | |
// create an instance | |
var notRealArray = new NotRealArray; | |
// instanceof | |
console.log( notRealArray instanceof Array ); // prints true, since instanceof just looks for "constructor" property in prototype chain | |
// Object.prototype.toString | |
console.log( ({}).toString.call( notRealArray ).indexOf('Array') !== -1 ); // prints false, ({}).toString.call() returns "[object Object]" here | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment