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

  • Home
  • SEARCH
  • 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 7784747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:08:41+00:00 2026-06-01T20:08:41+00:00

I am trying to extend a DIV via Javascript by using a newly created

  • 0

I am trying to “extend” a DIV via Javascript by using a newly created div as prototype of my object.

As I understand Javascript, on creating a new instance of my Object via “new”, the prototype-object is copied, assigned to “this” an then the function is executed (as the constructor).

Everything seems to work, except that whenever I create another object, and add it to the DOM, it “replaces” the original div. To be more exact: The constructor always changes the same div.

Using MyTest.prototype = document.createElement("div"); gives me the described behavior, the two commented lines after that in my code example are what I also tried, but to no avail.

I know trying to extend the DOM is frowned upon, but I want to understand this behavior, because I thought I knew how prototypes work and this simply does not fit my idea.

Here is a minimal example of what I am trying to do:

<!DOCTYPE html>
<html>
<head>
<title>Div-Prototype-Test</title>
<script type="text/javascript">

var height = 20;
var top = 0;

function MyTest() {
    var r = Math.floor(Math.random() * 256);
    var g = Math.floor(Math.random() * 256);
    var b = Math.floor(Math.random() * 256);

    this.style.backgroundColor = "rgb("+ r +","+ g +","+ b +")";
    this.style.position        = "absolute";
    this.style.width           = "500px";
    this.style.height          = height + "px";
    this.style.top             = top + "px";

    top += height;

    document.getElementsByTagName("body")[0].appendChild(this);
}

MyTest.prototype = document.createElement("div");
// MyTest.prototype = document.createElement("div").cloneNode(true);
// MyTest.prototype = new Element();

window.addEventListener(
    "load", 
    function() {
        var a = new MyTest();
        var b = new MyTest();
        var c = new MyTest();
        var d = new MyTest();
    }
);

</script>
</head>
<body>
</body>
</html>

PS: Because of a certain Javascript-Framework my search for anything that changes the prototype in Javascript always resulted in hundreds of results that had nothing to do with my problem – please tell me if I missed a question that already discusses this.

Edit:

To make my question clearer:

Here is an example where I use an object as prototype – its properties get copied.

function A() {
}

A.prototype = { property: 4 };

A.prototype.set = function(num) {
    this.property = num;
}

window.addEventListener(
    "load", 
    function() {
        var message = "";

        var x1 = new A();
        message += "A1 : "+ x1.property +"\n";

        x1.set(15);
        message += "A1 : "+ x1.property +"\n";

        var x2 = new A();
        message += "A2 : "+ x2.property +"\n";

        alert(message);
    }
);

The alert then said:

A1 : 4
A1 : 15
A2 : 4

The Div in my first example however does not seem to be copied, it behaves like a Singleton or Monostate. Should it not go like this?

  1. Protype object is copied into a new object
  2. the new object is assigned to “this”
  3. this is given to the constructor
  4. this is returned by the constructor (if no return statement is specified)
  • 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-06-01T20:08:42+00:00Added an answer on June 1, 2026 at 8:08 pm

    I found a workaround, it basically works the other way round – the prototype is a blank object and I copy the new objects data into a div in the constructor:

    var height = 20;
    var top = 0;
    
    function deepCopy(fromObject, toObject, depth) {
        if(typeof(fromObject) != "object" || typeof(toObject) != "object") {
            // throw "deepCopy only copies objects"
            return;
        }
    
        if (typeof(depth) == "undefined") {
            depth = 0;
        } else if (depth > 100) {
            // Recursion depth too high. Abort.
            // throw "deepCopy recursion depth cap hit"
            return;
        }
    
        for (var key in fromObject) {
            if (typeof(fromObject[key]) == "object" && fromObject[key] != null) {
                if (typeof(fromObject[key].nodeType) != "undefined") {
                    toObject[key] = fromObject[key].cloneNode(true);
                } else {
                    if (typeof(toObject[key]) != "object") {
                        toObject[key] = {};
                    }
                    deepCopy(fromObject[key], toObject[key], depth + 1);
                }           
    
            }
            toObject[key] = fromObject[key];
        }
    }    
    
    function MyTest() {
        // This is ugly...
        var self = document.createElement("div");
        deepCopy(MyTest.prototype, self);
    
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
    
        self.style.backgroundColor = "rgb("+ r +","+ g +","+ b +")";
        self.style.position        = "absolute";
        self.style.width           = "500px";
        self.style.height          = height + "px";
        self.style.top             = top + "px";
    
        top += height;
    
        document.getElementsByTagName("body")[0].appendChild(self);
    
        return self;
    }
    
    MyTest.prototype = {};
    // MyTest.prototype = document.createElement("div").cloneNode(true);
    // MyTest.prototype = new Element();
    
    window.addEventListener(
        "load", 
        function() {
            var a = new MyTest();
            var b = new MyTest();
            var c = new MyTest();
            var d = new MyTest();
        }
    );
    

    Although I have the feeling that my deepCopy-function is a rather inelegant (and possibly very buggy) way to perform the task, but the other way round with using cloneNode() did not work.

    My original problem came from this: When the prototype is copied, all scalar values are copied, while all objects are simply referenced (like copying pointers, the pointer value is duplicated, but not the data it points to).

    Hope this helps someone.

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

Sidebar

Related Questions

I'm trying to extend the SPL ArrayObject but I've hit a little snag. Using
I'm trying to extend the JavaScript on a page I maintain to apply some
I am trying to extend the mysqli class to make a helper class that
I'm trying to extend JButton with Clojure, but I ran into a problem when
I'm trying to extend a form and add an upload file field from within
I am trying to extend the Upload library in CodeIgniter. I have been trying
I'm trying to extend my ActiveRecord class with some dynamic methods. I would like
I'm trying to extend TextBox control to add watermarking functionality. The example I've found
I'm trying to extend the text input that comes in flex to support an
I'm trying to extend jWysiwyg with an function to add a map from Google

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.