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 7178495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:55:56+00:00 2026-05-28T16:55:56+00:00

This code work : function class1(){ this.x5 = 5; this.x6 = 6; this.prototype =

  • 0

This code work :

    function class1(){
        this.x5 = 5;
        this.x6 = 6;
        this.prototype = 5;
    }

    function class2(){
        this.x3 = 3;
        this.x4 = 4;
    }


    class2.prototype = new class1();
    var obj1 = new class2();
    alert(obj1.x5 ); // alert me 5 

But why this not working :

     function class1(){
        this.x5 = 5;
        this.x6 = 6;
        this.prototype = 5;
     }

    function class2(){
        this.x3 = 3;
        this.x4 = 4;
        this.prototype = new class1();  // or class2.prototype = new class1(); 
     }

    var obj1 = new class2();
    alert(obj1.x5); // alert me "undefinded"
  • 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-28T16:55:58+00:00Added an answer on May 28, 2026 at 4:55 pm

    You can’t set the prototype inside of the function like that. When invoking a function with the new operator, then a new object is created, and this gets set to that object. Objects don’t have a publicly accessible prototype property that you can set. Their prototype property is actually __proto__, which is not accessible (although some browsers do let you get at it).

    In your second example, you’re just setting a plain ol’ vanilla property named "prototype" with a value.

    the prototype property on a function is also not the prototype! Confusing, eh? What it really is is something of a "prototype template". It basically means "when you create an object using this function as the constructor, set their prototype to whatever I have set in the prototype property." This can be very confusing until you grok it.

    Also Note

    Your first example doesn’t work either (try it here), you are setting the prototype property of the function after the instance was already created. So that instance was already given a different prototype object. If you created a second instance of class2, it would alert the property correctly.

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

Sidebar

Related Questions

Does anyone know why this code doesn't work. This means, the alert is NOT
I know this will work: function Foo() {}; Foo.prototype.talk = function () { alert('hello~\n');
following up from yesterday... This portion of the code does work. $(document).ready(function(){ $('#listMenu a').click(function
I thought this code would work, but the regular expression doesn't ever match the
This code used to work but doesnt any more. i used a breakpoint, and
I am unsure why this code will not work. When I click a button
I have the following code: (function(){ var DS = (function(){ DS.prototype.queryDB = function() {
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
MyClass c = 10; Is there any way to make this code work? I

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.