Skip to content

Instantly share code, notes, and snippets.

@kristopher
Forked from jeresig/isObjectLiteral.html
Created July 24, 2009 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopher/154560 to your computer and use it in GitHub Desktop.
Save kristopher/154560 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<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){
//Adapted from eligrey's gist http://gist.github.com/153845
// Tested in FF 3.5(OSX), Safari 4(OSX), Chrome(XP), Opera 9.6(XP), IE 6(XP), IE 7(XP)
if (String(obj) != '[object Object]') {
return false;
} else {
var hasOwnProperty = Object.prototype.hasOwnProperty;
for(var property in obj) {
if(!hasOwnProperty.call(obj, property) && !hasOwnProperty.call(Object.prototype, property)) {
return false;
}
}
}
return true;
}
// Function serialization is not permitted
// Does not work across all browsers
Function.prototype.toString = function(){};
log("null", null, false);
log("undefined", undefined, false);
log("[]", [], false);
log("string", 'string', false);
log("true", true, false);
log("1", 1, false);
log("window", window, false);
log("document", document, false);
log("element", document.body, false);
// 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(){};
// Makes the function a little more realistic
// (and harder to detect, incidentally)
fn.prototype = {someMethod: function(){}};
// Functions shouldn't be matched
log("fn", fn, false);
// Again, instantiated objects shouldn't be matched
log("new fn", new fn, false);
var fn_empty_prototype = function() {};
fn_empty_prototype.prototype = {};
// Fails... I don't know of any way to check for this other than getPrototypeOf
// which isn't avaliable in IE.
log("new fn_empty_prototype(fails...)", new fn_empty_prototype, 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