Questions tagged [prototypal-inheritance]

Prototype inheritance reuses behavior by cloning existing objects that serves as prototypes. JavaScript is a very good example of prototype-based inheritance in practice.

Useful links

843 questions
1054
votes
3 answers

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

The API Reference Scope page says: A scope can inherit from a parent scope. The Developer Guide Scope page says: A scope (prototypically) inherits properties from its parent scope. So, does a child scope always prototypically inherit from its…
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
990
votes
34 answers

__proto__ VS. prototype in JavaScript

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, and which in turn also references via its __proto__ property again to the Object.prototype. Thus, repeat,…
0x90
  • 39,472
  • 36
  • 165
  • 245
174
votes
9 answers

Why is extending native objects a bad practice?

Every JS opinion leader says that extending the native objects is a bad practice. But why? Do we get a perfomance hit? Do they fear that somebody does it "the wrong way", and adds enumerable types to Object, practically destroying all loops on any…
buschtoens
  • 8,091
  • 9
  • 42
  • 60
141
votes
6 answers

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

If I understand correctly, each and every object in JavaScript inherits from the Object prototype, which means that each and every object in JavaScript has access to the hasOwnProperty function through its prototype chain. While reading RequireJS'…
timkg
  • 1,773
  • 2
  • 14
  • 12
89
votes
11 answers

Good Example of JavaScript's Prototype-Based Inheritance

I have been programming with OOP languages for over 10 years but I'm learning JavaScript now and it's the first time I've encountered prototype-based inheritance. I tend to learn fastest by studying good code. What's a well-written example of a…
Alex Reisner
  • 29,124
  • 6
  • 56
  • 53
73
votes
5 answers

How to access object prototype in javascript?

In all the articles it is written that JavaScript is a prototype-based language, meaning that every object has a prototype (or, more precisely, prototype chain). So far, I've tried the following code snippet: var F = function(); F.prototype.member1…
BreakPhreak
  • 10,940
  • 26
  • 72
  • 108
50
votes
2 answers

How do I inherit javascript functions ?

// Don't break the function prototype. // pd - https://github.com/Raynos/pd var proto = Object.create(Function.prototype, pd({ "prop": 42 })); var f = function() { return "is a function"; }; f.__proto__ =…
Raynos
  • 166,823
  • 56
  • 351
  • 396
43
votes
4 answers

Prototypical OO in JavaScript

TL;DR: Do we need factories/constructors in prototypical OO? Can we make a paradigm switch and drop them completely? The BackStory: I've been toying with doing prototypical OO in JavaScript lately and find that 99% of OO done in JavaScript is…
Raynos
  • 166,823
  • 56
  • 351
  • 396
38
votes
7 answers

Proper way to call superclass functions from subclass

I have a "SuperClass" with "info" as an instance variable. "SuperClass" has function "printInfo()". "printInfo()" needs to access instance variable "info". I want to create a "SubClass" which also has method "printInfo()". I want to call…
coolscitist
  • 3,317
  • 8
  • 42
  • 59
36
votes
7 answers

Extending the defaults of a Model superclass in Backbone.js

I would like to pose this as a question to this answer but I can't seem to do so, I apologize. Extending the defaults for the subclass are reflected in the superclass. This seems to defeat the purpose and I'm more apt to explicitly list the…
mcdoh
  • 463
  • 1
  • 4
  • 6
29
votes
7 answers

Convert javascript class instance to plain object preserving methods

I want to convert an instance class to plain object, without losing methods and/or inherited properties. So for example: class Human { height: number; weight: number; constructor() { this.height = 180; this.weight = 180; …
eAbi
  • 3,220
  • 4
  • 25
  • 39
28
votes
8 answers

(Open Source) Examples of JavaScript Prototypical OO

Bounty Edit: I'm looking for code written in a pure prototypical OO paradigm (think Self). Not a mixture of prototypical OO and classical OO. I don't want to see generic OO wrappers but simply usage of prototypical OO techniques and only…
Raynos
  • 166,823
  • 56
  • 351
  • 396
28
votes
3 answers

Check if a constructor inherits another in ES6

I have a situation where I need to check if a constructor (X) has another constructor (Y) in its prototype chain (or is Y itself). The quickest means to do this might be (new X()) instanceof Y. That isn't an option in this case because the…
Semicolon
  • 6,793
  • 2
  • 30
  • 38
28
votes
5 answers

Properties of Javascript function objects

I have a JavaScript function object as; var addNum = function(num1, num2) { return num1 + num2; } Now if I try to access addNum.divide() I wanted to understand the prototype chain for the above code. I read that in the above example,…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
25
votes
4 answers

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

I'm building a Node.js app with Connect/Express.js and I want to intercept the res.render(view, option) function to run some code before forwarding it on to the original render function. app.get('/someUrl', function(req, res) { res.render…
1
2 3
56 57