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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:06:36+00:00 2026-05-30T00:06:36+00:00

I have a class definition method for create class in javascript: var Class =

  • 0

I have a class definition method for create class in javascript:

var Class = function() {
    var clazz = null,
    pros = {};
    for (var i = 0; i < arguments.length; i++) {
        var arg = arguments[i];
        if (typeof arg == "function") arg = arg.prototype;
        else {
            arg.init && (clazz = arg.init, delete arg.init)
        }
        for (var p in arg) pros[p] = arg[p];
    }
    clazz.prototype = pros;
    return clazz;
};

var Person = Class({
    init: function(name) {
        this.name = name;
    },
    say:function(){
        console.info(this.name);
    }
});

var Man = Class(Person, {
    init: function(name) {
        Person.apply(this, arguments);
        this.gender = 'man'
    }
});

var m = new Man('kk');
console.info(m instanceof Man);
console.info(m instanceof Person);

However, it does not support the instanceof operator.

Any idea to fix it?

  • 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-30T00:06:38+00:00Added an answer on May 30, 2026 at 12:06 am

    You should keep track of the prototype chain to make instanceof work. What you’re currently doing is merely copying properties into one object, and use that as prototype of the returned function. As a result, the information that Parent is a parent of Man is lost.

    You’d need to set the prototype instead of copying. Since you need to modify an existing object to set the underlying prototype of, the deprecated __proto__ property is required. Object.create cannot be used here, because that returns a new object.

    Edit: It is possible without __proto__, but you’d need the function F trick: http://jsfiddle.net/p9pvQ/.

    var clazz = null,
        pros = Object.prototype; // root of chain
    
    for (var i = 0; i < arguments.length; i++) {
        var arg = arguments[i];
    
        if (typeof arg === "function") {
            arg = arg.prototype;
        } else {
            if(arg.init) {
                clazz = arg.init;
                delete arg.init;
            }
            var o = arg;
            arg = (function() { function F() {}; F.prototype = pros; return new F; })();
            for(var key in o) arg[key] = o[key];
        }
    
        pros = arg;
    }
    

    m will then have the prototype chain as follows:

    Class.init // m
      gender: "man"
      name: "kk"
      __proto__: F // Man
        __proto__: F // Person
          say: function (){
          __proto__: Object // Object.prototype
            __defineGetter__: function __defineGetter__() { [native code] }
            __defineSetter__: function __defineSetter__() { [native code] }
            ... (Object.prototype functions)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I want to create a javascript class/object/function which have a method that
I have a the following method definition in my class: virtual Calc* Compile( Evaluator*
Let's have the following class definition: CThread::CThread () { this->hThread = NULL; this->hThreadId =
I have an class definition with a __hash__ function that uses the object properties
Say I have a class definition: class CustomClass { int member; }; Why is
Anyone knows if is possible to have partial class definition on C++ ? Something
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
Let's say I have an object who's class definition looks like: class Command: foo
I have class method that returns a list of employees that I can iterate
I just came across an interesting situation in JavaScript. I have a class with

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.