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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:09:30+00:00 2026-06-17T21:09:30+00:00

I created a read only property on an object and the appropriate unit tests.

  • 0

I created a read only property on an object and the appropriate unit tests.

//works as expected
function OnSelf() {
    this._val = 'test';
    Object.defineProperty(this, 'test', {
        enumerable: true,
        configurable: false,
        get: function () {
            return this._val;
        }
    });
} 

However, I realized that I should have been putting the readonly property on the prototype instead of each individual instance. I changed my code and then one of my tests failed.

//no exception when trying to delete the property
function OnPrototype() {
    this._val = 'test';

}
Object.defineProperty(OnPrototype.prototype, 'test', {
    enumerable: true,
    configurable: false,
    get: function () {
        return this._val;
    }
});

It appears that when deleting a read-only property on the prototype, no exception is thrown, but when the property is on the object, an exception is thrown.

var s = new OnSelf();
delete s.test; // throws error

var p = new OnPrototype();
delete p.test; // doesn't delete it, but no error occurs

I created http://jsfiddle.net/pdgreen/3BGfM/ to demonstrate the problem. I confirmed the same behavior on Mac with chrome and firefox.

Is this the correct thing to happen? Why if the property is on the object, an exception is thrown, but on the prototype, no exception? This surprises me. Can anyone explain why it is so?

  • 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-17T21:09:31+00:00Added an answer on June 17, 2026 at 9:09 pm

    That’s the correct behavior you’re seeing. The ‘delete’ keyword only deletes an object’s own properties; it can’t delete the object’s prototype’s properties. (And if it did, that would be terrible — it would mess up any other objects inheriting from that same prototype!)

    Try the following:

    > function Constructor() {}
    undefined
    > Constructor.prototype.test = "Prototype";
    "Prototype"
    > var obj = new Constructor();
    undefined
    > obj.test
    "Prototype"
    > obj.test = "Child"
    "Child"
    > obj.test
    "Child"
    > delete obj.test
    true
    > obj.test
    "Prototype"
    

    The key to prototypical inheritance is that prototypes are actual objects containing their own properties, and that inheritance is fully live and dynamic. If an object doesn’t define a property but an object in its prototype chain does, the child object inherits that value. When its value gets overridden locally, it now defines that property itself. If the child object’s property gets deleted, the value from the prototype comes through again.

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

Sidebar

Related Questions

How to create shared folder in C# with read only access? I see this
I've got a class that has a read-only property defined that is actually a
I want to create a decorator that works like a property, only it calls
In my mind the ID field of a business object should be read-only (public
I have a class with a read-only property defined. In my code I define
I want to create a read-only user in PostgreSQL. The intention is to have
I'm looking to create a read-only table, and MYSQL permissions, I think, are the
Many library classes in AS3 have read only properties. Is it possible to create
How do I create a MS SQL server user that only allowed to read
I have a data structure created using read.csv() of dimensions 130, 395. I am

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.