-
-
Save hdragomir/153744 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html;charset=utf-8"/> | |
<!-- Online here: http://ejohn.org/files/bugs/isObjectLiteral/ --> | |
<title>isObjectLiteral</title> | |
<style> | |
li { background: green; } li.FAIL { background: red; } | |
iframe { display: none; } | |
</style> | |
</head> | |
<body> | |
<ul id="results"></ul> | |
<script> | |
function isObjectLiteral(obj){ | |
// Implement me! | |
return false; | |
// Other Solutions: | |
// Uses function serialization which doesn't work in all browsers | |
//return /^function Object/.test( obj.constructor ); | |
// Doesn't work with object from other windows | |
//return Object.prototype.toString.call(obj) === "[object Object]"; | |
} | |
// Function serialization is not permitted | |
// Does not work across all browsers | |
Function.prototype.toString = function(){}; | |
// The use case that we want to match | |
log("{}", {}, true); | |
// Instantiated objects shouldn't be matched | |
log("new Date", new Date, false); | |
var fn = function(){}; | |
// Functions shouldn't be matched | |
log("fn", fn, false); | |
// Again, instantiated objects shouldn't be matched | |
log("new fn", new fn, false); | |
/* Note: | |
* The restriction against instantiated functions is | |
* due to the fact that this method will be used for | |
* deep-cloning an object. Instantiated objects will | |
* just have their reference copied over, whereas | |
* plain objects will need to be completely cloned. | |
*/ | |
var iframe = document.createElement("iframe"); | |
document.body.appendChild(iframe); | |
var doc = iframe.contentDocument || iframe.contentWindow.document; | |
doc.open(); | |
doc.write("<body onload='window.top.iframeDone(Object);'>"); | |
doc.close(); | |
function iframeDone(otherObject){ | |
// Objects from other windows should be matched | |
log("new otherObject", new otherObject, true); | |
} | |
function log(msg, a, b) { | |
var pass = isObjectLiteral(a) === b ? "PASS" : "FAIL"; | |
document.getElementById("results").innerHTML += | |
"<li class='" + pass + "'>" + msg + "</li>"; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment