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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:10:21+00:00 2026-05-24T22:10:21+00:00

I am taking my first OOP class (learning Java) in the fall so I’ve

  • 0

I am taking my first OOP class (learning Java) in the fall so I’ve been reading up about it and going through little introductions to the language to get myself acquainted before school. I’m very excited to learn Java but feel I’m more ‘at home’ with JavaScript. I’ve done a few projects with it and am working on taking my code to the next, more professional level (if you want to say that) by using OOP in JS.

Currently I am working on dragging script and have it pretty much pinned down with procedural programming. Since this project is fresh in my memory, I am starting with this project to begin my move to objects. What is tripping me up here is the idea of having a ‘Drag’ object . . .

function Drag() {
    /* Drag code goes here */
} // constructor

I’m taking fairly well to the idea of OOP, but this stumps me. In all the examples I’ve gone through, the objects were always, well, objects. They were animals or cars, or a bike, etc. I have yet to come across the object of a behavior. I’m just not sure how it would work . . . If any of you have any advice on this, I’d appreciate it.

Now for inheritance! I haven’t had to use inheritance quite yet, but the subject interests me with JavaScript because so many people have put their 2 cents in about it. I think the most popular ways, though, that I’ve seen are John Resig’s and Douglas Crockford’s ways of inheritance.

I think they are very neat workarounds but for some reason I don’t really like that they both require assignment. I guess I just feel like they shouldn’t have to work that way. So, after looking through some more examples and at Crockford’s method, I came up with this (very sorry if someone else already has, I just haven’t seen it):

Object.prototype.extend = function (Obj) {
    'use strict'; // for jslint

    this.prototype = new Obj();
    this.prototype.constructor = this;
    return this;
}

Here is an example of it in use, I stole the example from here then modified it a bit:

Object.prototype.inObj = 1;

function A() {
    'use strict';
    this.inA = 2;
}

A.prototype.inAProto = 3;

function B() {
    'use strict';
    this.inB = 4;
}

/*** HERE IT IS ***/
B.extend(A);

B.prototype.inBProto = 5;

var w = new A();
var x = new B();

document.body.innerHTML = x.inObj + ', ' + x.inA + ', ' + x.inAProto + ', ' + x.inB + ', ' + x.inBProto; // 1, 2, 3, 4, 5
document.body.innerHTML += '<br />';
document.body.innerHTML += w instanceof A && w instanceof Object && x instanceof B && x instanceof A && x instanceof Object; // true

To me it looks and feels syntactically nice, I wonder how seasoned JS developers would feel about it. I tested it, and I know that you can place B.extend(A) before the definition for B but you cannot place it after B.prototype.inBProto or the list will become 1, 2, 3, 4, undefined because in the extend method, B‘s prototype is being reset. I’m working on a way around this but feel that B.extend(A) should come directly after the Constructor’s definition anyway.

What do you all think of this way? Thanks in advance!

EDIT: I developed a [clumsy] workaround which basically takes all the properties of the inheriting object’s prototype and then adds them back on after it is wiped clean by the new object:

Object.prototype.extend = function (Obj) {
    'use strict';
    var proto, p;

    proto = {};
    for (p in this.prototype) {
        if (this.prototype.hasOwnProperty(p)) {
            proto[p] = this.prototype[p];
        }
    }

    this.prototype = new Obj();
    this.prototype.constructor = this;

    for (p in proto) {
        if (proto.hasOwnProperty(p)) {
            this.prototype[p] = proto[p];
        }
    }

    return this;
};

Who knows, perhaps you won’t think this is quite as clumsy as I do, but it works and I think it’s pretty cool! :D

  • 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-24T22:10:21+00:00Added an answer on May 24, 2026 at 10:10 pm

    I agree with Daniel, you should never extend the basic JS core.

    The reason I can break is that someone else may have done the same. But it’s more about not changing the default behavior for core code.

    I would use a different approach. Instead for extending the Object make it a function and the use apply.

    Made a simple mockup: http://jsfiddle.net/djEw8/

    Read about apply here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/function/apply

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

Sidebar

Related Questions

I'm taking my first steps toward learning to write desktop code for the Mac
I'm taking my first steps in oop, and right now I'm re-building a practice
I'm taking my first steps through Unity, as a follow up to this question:
I am a computer science student and taking my first C++ class. I have
Some preface: I'm a computer engineering student taking a first class in C after
I'm taking my first steps in python programming, I have a class named node
I am taking my first steps programming in Lua and get this error when
I am taking my first foray into PHP programming and need to configure the
I'm just taking my first steps with Azure and the first thing I see
first of all thanks for taking your time! I'm a junior Dev, working 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.