Prototypical OO in JavaScript

I don’t think the constructor/factory logic is necessary at all, as long as you change how you think about Object-Oriented Programming. In my recent exploration of the topic, I’ve discovered that Prototypical inheritance lends itself more to defining a set of functions that use particular data. This isn’t a foreign concept to those trained in …

Read more

How to access object prototype in javascript?

var f = function(); var instance = new f(); If you know name of instance class function, you can simply access prototype as: var prototype = f.prototype; prototype.someMember = someValue; If you don’t: 1) var prototype = Object.getPrototypeOf(instance); prototype.someMember = someValue; 2) or var prototype = instance.__proto__; prototype.someMember = someValue; 3) or var prototype = …

Read more

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

Is there any practical difference [between my examples]? The user may have a JavaScript object created with Object.create(null), which will have a null [[Prototype]] chain, and therefore won’t have hasOwnProperty() available on it. Using your second form would fail to work for this reason. It’s also a safer reference to Object.prototype.hasOwnProperty() (and also shorter). You …

Read more

What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

Quick answer: A child scope normally prototypically inherits from its parent scope, but not always. One exception to this rule is a directive with scope: { … } — this creates an “isolate” scope that does not prototypically inherit. This construct is often used when creating a “reusable component” directive. As for the nuances, scope …

Read more