prototypal inheritance concept in javascript as a prototype based language

Classical inheritance is about extending types of things. Say you have a class, like Bike. When you want extend the behaviour, you have to design a new type of bike (like MotorBike). It’s like building a factory – you make lots of blueprints, and blueprints that reference those blueprints, but in order to ride one … Read more

Extending core types without modifying prototype

2 years later: mutating anything in global scope is a terrible idea Original: There being something “wrong” with extending native prototypes is FUD in ES5 browsers. Object.defineProperty(String.prototype, “my_method”, { value: function _my_method() { … }, configurable: true, enumerable: false, writeable: true }); However if you have to support ES3 browsers then there are problems with … Read more

Is John Resig’s Javascript inheritance snippet deprecated?

Does that mean I shouldn’t use John Resig’s code? Correct, not when you are using ES5 in strict mode. However, it can be easily adapted: /* Simple JavaScript Inheritance for ES 5.1 * based on http://ejohn.org/blog/simple-javascript-inheritance/ * (inspired by base2 and Prototype) * MIT Licensed. */ (function(global) { “use strict”; var fnTest = /xyz/.test(function(){xyz;}) ? … Read more

Node.js / Express.js – How to override/intercept res.render function?

Old question, but found myself asking the same thing. How to intercept res render? Using express 4.0x something now. You can use/write middleware. The concept was a bit daunting to me at first, but after some reading it made a little more sense. And just for some context for anyone else reading this, the motivation … Read more

Object.defineProperty in ES5?

There are several things that you can’t emulate from the ECMAScript 5 Object.create method on an ECMAScript 3 environment. As you saw, the properties argument will give you problems since in E3-based implementations there is no way to change the property attributes. The Object.defineProperty method as @Raynos mentioned, works on IE8, but partially, it can … Read more

Performing inheritance in JavaScript

The JavaScript object oriented paradigm is prototype based. There are no “classes”, just objects. You can implement inheritance in different ways. The two more popular alternatives are the “pseudo-classical” and the “prototypal” forms. For example: Pseudo-classical inheritance I think this is the most popular way. You create constructor functions that you use with the new … 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

Grasping prototypical Inheritance through pseudoclassical instantiation (JavaScript)

When using constructor functions for inheritance in JavaScript, you: Make the prototype property of the “derived” constructor an object whose prototype is the prototype property of the “base” constructor. Set the constructor property on the “derived” constructor’s prototype property to point to the “derived” constructor. Call the “base” constructor from the “derived” constructor with the … Read more