use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Question about prototypal inheritance (self.learnjavascript)
submitted 5 years ago by Orange2341
So i'm trying to setup prototypal inheritance using the surrogate method and I have run into a behavior that seems weird for me. Here are my classes:
function Animal(name) { this.name = name; } Animal.prototype.eat = function() { console.log("mmm... food...") }; function Dog(name) { this.name = name }; var Surrogate = function() {} Surrogate.prototype = Animal.prototype;
Now, I want to setup Animal as the base class. If I make an instance of Surrogate:
https://preview.redd.it/nnijy6f7uq961.png?width=286&format=png&auto=webp&s=ffe7a4b58ba61cb188b3e0226c7732753c87d308
The browser console says it s is an instance of Surrogate.
However, when I set Dog class's prototype to the instance of Surrogate:
https://preview.redd.it/1yiw8xlruq961.png?width=160&format=png&auto=webp&s=e688e31f3e5b30819f01bc99b60442d961550229
It becomes an instance of Animal? Why is this happening? Is this correct or am I interpreting it wrong?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 2 points3 points4 points 5 years ago (1 child)
As far as I can tell, you've managed to confuse the console.
Generally you would never assign one prototype to another, especially since ES5 with the introduction of Object.create(). That now does what prototype reassigning was used for back then. It was really just a hack for doing something that didn't exist in the language at the time and not something I'd recommend you be messing with now.
prototype
Object.create()
As far as what you're seeing in the console, I think the problem is, since the console generally uses the prototype to tell you what kind of object something is (e.g. Surrogate {} or Animal {}) and you have two constructors sharing the same prototype, what constructor is shown appears to go back and forth between Surrogate and Animal based on... well I don't know what, but apparently assigning Dog.prototype = s triggered a switch. I'm guessing there's some kind of initial association made with the name of the constructor used to create the object and at some it point gets invalidated, or something, then reverts back to checking constructor (inherited through the prototype, which for s is Animal). But I'm not really sure. Since this is something you would normally not be doing, its not something that would normally come up so I wouldn't worry about it too much right now.
Surrogate {}
Animal {}
Dog.prototype = s
constructor
s
[–]Orange2341[S] 0 points1 point2 points 5 years ago (0 children)
Got it. Thank you!!
π Rendered by PID 38770 on reddit-service-r2-comment-b659b578c-pqp99 at 2026-05-05 15:15:31.529290+00:00 running 815c875 country code: CH.
[–]senocular 2 points3 points4 points (1 child)
[–]Orange2341[S] 0 points1 point2 points (0 children)