Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:33:29+00:00 2026-05-16T23:33:29+00:00

I was experimenting with inheritance in javascript, and wrote those two functions: Object.prototype.inherits=function(obj){this.prototype=new obj;}

  • 0

I was experimenting with inheritance in javascript, and wrote those two functions:

Object.prototype.inherits=function(obj){this.prototype=new obj;}
Object.prototype.pass=function(obj){obj.prototype=new this;}

This code works very well:

Dog.inherits(Animal);

But the following fails:

Animal.pass(Dog);

As I understand it, my pass functions doesn’t work, because “this” isn’t a reference to the object instance itself? If that’s the case, how can I reference the object from within itself?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:33:30+00:00Added an answer on May 16, 2026 at 11:33 pm

    Well, actually the two are doing exactly the same:

    Dog.prototype = new Animal;
    

    The this value inside the methods will refer to the base object where the reference was invoked, in the case of:

    Dog.inherits(Animal);
    

    The this value will refer to the Dog constructor function, and the obj argument will be the Animal function.

    When you call:

    Animal.pass(Dog);
    

    The this value will refer to the Animal function, doing at the end exactly the same thing as the inherits method, but the other way around.

    I would recommend you to not extend the Object.prototype object, because it can cause you a lot of problems, for example those two properties will be enumerated in any for-in loop, e.g.:

    for (var prop in {}) { // <-- an empty object!
      alert(prop); // will alert 'inherits' and 'pass'
    }
    

    All objects inherit from Object.prototype, and it seems that you intend to use those methods only on Function objects, it would be safer to extend the Function.prototype object, or implement the methods as functions that take two parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experimenting with JavaScript Inheritance. Basically, I am following this tutorial. I see
Was experimenting with interface inheritance and found this: interface String{} interface Object{} interface Exception{}
Am experimenting with an anonymous function : var a:Object = new Object() ; a.b
ok, I was experimenting (getting straight in my head) all this object oriented like
i've been experimenting with javascript's prototypal inheritance and have come across something that perhaps
I'm experimenting with TextArea and inheritance to implement some additional functionality on the protected
I was experimenting with shell functions the other day, with the objective of overriding
While experimenting a bit with C++ templates I managed to produce this simple code,
Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting
Experimenting with Cocos2D collection detection and have some questions. First, some background: This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.