Skip to content

Instantly share code, notes, and snippets.

@timkg
Created December 12, 2014 11:57
Show Gist options
  • Save timkg/39f7b702bfdcd89de501 to your computer and use it in GitHub Desktop.
Save timkg/39f7b702bfdcd89de501 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<p>Most object-oriented languages are actually class-based.</p>
<p>Javascript is object-oriented in the sense that you can create Objects without Classes.</p>
<p>Every Object in JS is created via a Constructor, implicitly or explicitly.</p>
<p>A Constructor creates a new Object with a link to the Constructor's prototype.</p>
<p>The constructor function "owns" the prototype. The object is linked to that prototype behind the scenes.</p>
<p>Defining a function creates the function and an object. The function receives a prototype property which points to the object. The object receives a constructor property which points to the function. These names are arbitrary - the object is not constructed by invoking the function. It's just created along the function definition, and can reference the F via a property set on the Object, by chance named constructor.</p>
<p>Four things happen when a function is called with the new keyword:
<ol>
<li>A brand new object is created</li>
<li>execution context (what this means inside the function) is set to the new object</li>
<li>The new object is linked behind the scenes to the function's prototype property</li>
<li>this is returned from the function</li>
</ol>
</p>
<p>Creating an object using a constructor function called with new DOES NOT give the new object a prototype property. It also DOES NOT give the object a constructor property. It only links the object behind the scenes to the function's prototype property, whatever value it might have.</p>
<p>There are three way to get an object's [[Prototype]], the object at the other end of an objects behind-the-scenes prototype link:
<ol>
<li>Using the non-standard obj.__proto__ property</li>
<li>Using the ES5 standardized Object.getPrototypeOf(obj) method, available with IE9 and above</li>
<li>Using the accidental, insecure obj.constructor.prototype link (obj.constructor doesnt exist, follows [[Prototype]] link, checks for constructor there, follows to constructor function, and constructor function has a property named prototype which points to [[Prototype]]. This method is highly insecure because all of these properties (constructor and prototpye) are writable. They can just be overwritten, and the link is lost.</li>
</ol>
</p>
<p>What is a constructor? - A function called with new</p>
<p>What is a [[Prototype]]? - A link between two objects. Can be set via constructor function's prototype property or Object.create</p>
<p>How does [[Prototype]] affect an object? - property lookup follows [[Prototype]] links</p>
<p>How do we find an object's [[Prototype]]? 1 - Object.getPrototypeOf(obj), 2 - obj.__proto__ (not standard), 3 - obj.constructor.prototype (insecure, can be overwritten, doesnt work with Object.create.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment