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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:58+00:00 2026-05-29T11:08:58+00:00

I’m trying to extend (not sure what is the correct term for this kind

  • 0

I’m trying to “extend” (not sure what is the correct term for this kind of extensions) a DOMElement to override certain properties (base element). To do so I dynamically create a function whose prototype is the DOMElement I want to extend, and then instance an object with that function (extended element).

When assigning a value to the extended element the expected outcome would be that a property with the given name is created in the extended element, leaving the base element untouched, even if it has a property with that same name. Doing so with a plain object as a base element works flawlessly, but when using a DOMElement as the base element, the base element is modified if a property by the same name exists (On Chrome and Firefox. On IE9 an “Invalid calling object” is thrown even when trying to get the property’s value).

Test code:

var base = $("<input type='text' />")[0]; 
//Plain object example: var base = {value:null}
base.value = "original";

var MyClass = function(){ };
MyClass.prototype = base;

var obj = new MyClass();
obj.value = "modified";

obj.value    //"modified" (OK)
base.value   //"modified" (WRONG!) - "original" if plain object is used (OK)

Application:

What I intended to do was to create an extended instance of an input element with an override “value” attribute. This would allow passing this object to a validator plugin that would use the overridden value to run all of it’s validation rules, and in case of any error the original object would remain intact, and the extended one with all the modifications done by the validator.

Due to the probably unsolvable exception thrown in IE we are not going to further apply this method, but I’m still intrigued by this strange behavior.

What the problem might be:

I think that this behavior comes from the fact than a DOMElement attributes and functions are not pure Javascript, but an exposition of an Interface of the browser implementation. Being so affects how the prototype “inheritance” in this case works, making the native setter implementation for the value to act instead of the creation of the property on the extended object. (This can be supported by the exception thrown in IE, stating that I’m trying to access a native function/property from a non-native element).

  • 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-29T11:08:58+00:00Added an answer on May 29, 2026 at 11:08 am

    This seems to have done the trick:

    var base = $("<input type='text' />")[0];
    base.value = "original";
    
    function MyClass() {
      Object.defineProperty(this, "value", {
        value: null,
        configurable: true,
        enumerable: true,
        writable: true
      });
    }
    MyClass.prototype = base;
    
    var obj = new MyClass();
    obj.value = "modified";
    
    console.log(obj.value);    //"modified" (OK)
    console.log(base.value);   //"original" (OK)
    

    The problem is that your obj instance never had its own value: it was always delegating to the prototype’s setter, i.e. to HTMLInputElement.prototype.value. So all instances of obj would share this same value property.

    In other words, saying obj.value = "modified" was the same as saying Object.getPrototypeOf(obj).value = "modified", and of course Object.getPrototypeOf(obj) === base.

    My first try was to give MyClass its own per-instance value property, like so:

    function MyClass() {
        this.value = null;
    }
    

    But this doesn’t actually do what we wanted, because this.value just references the prototype’s value: we’re not creating a new property; we’re setting the existing one. Object.defineProperty makes it very explicit to the runtime that we want to create a new one, though, so that does the trick.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.