<div>
<p>Exploring the object <code>constructor</code> property, and the <code>instanceof</code> operator. See the code comments for the explanation </p>
<p>Output will appear below:</p>
</div>
<div id="output"></div>
// our Person constructor
function Person(fname, lname) {
this.fname = fname;
this.lname = lname;
}
// now we make an object from the Person object type
var superman = new Person("Clark","Kent");
// Here we see that Javascript has assigned the
// constructor property, which points to the
// constructor function itself
logMessage("Superman's constructor object:<br><code> "+superman.constructor + "</code><hr>");
// Superman is still an Object - remember that there's
// no real class-type inheritance in Javascript
logMessage("Superman is an: "+ typeof superman);
// But because of the 'constructor' property, Javascript can
// tell us that superman is an 'instanceof' Person
logMessage("Superman <code>instanceof</code> "+superman.constructor.name+": "+ (superman instanceof Person));
// Utility function for logging convenience
// Logs msg to the element with given id
// If id is undefined, logs to #output
function logMessage(msg, id){
if (!id){
id="output";
}
document.getElementById(id).innerHTML += msg + "<br>";
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.