Jump to content

The delete keyword does not delete inherited properties,...but i just have!!


Rod

Recommended Posts

Quote from Parent website

"Prototype Properties"

"JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype."

so why does the script below work?

<script>
function Person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
var myMother = new Person("Sally", "Rally", 48, "green");

delete myMother.age; // apparently... this should not work as "age" (as I understand) has been inherited from the Person prototype.

document.getElementById("demo").innerHTML =
"My father is " + myFather.age + ". My mother is " + myMother.age; 
</script>
myMothe.age  now outputs undefined and that's what i expected.

What am i misunderstanding?

Thank you in advance.

Link to comment
Share on other sites

Might it be that you have not created a prototype?

What you have done is created a function.

This might be a good starting point.

Link to comment
Share on other sites

The script above was copied from this website, under the heading "creating a prototype" and in there is says to use "object constructor function" which is what I have done.

Now this.

The link you gave shows how to create new methods to pre-existing  javascript object. (in your case..... an Array).

Link to comment
Share on other sites

I don't see any inheritance in your code. And why would you want to delete a property anyway?

myMother is a Person. That isn't inheritance.

If you are learning Javascript as a newbie I would not worry about this topic yet, because it is a rather sticky issue, and Javascript is in the middle of an upgrade (es6) to a more standard way of handling inheritance.

Link to comment
Share on other sites

There no real reason, just trying to understand what I've learnt and might be good to know for the future..

I assumed that because "age" property is in the "Person" object prototype, then the myMother object would "inherit" the "age"  property. So... with reference to your last comment, I must be misunderstanding the "inherit" thing.

Link to comment
Share on other sites

Inheritance in most languages is when you have a Person class and also an Employee class. You can then have Employee inherit Person since every employee is also a person. Classic Javascript has a prototype scheme that is rather strange. You can google "Javascript inheritance" to find various discussions of both the classic Javascript and the new es6/es2015 approaches.

Link to comment
Share on other sites

Quote

I assumed that because "age" property is in the "Person" object prototype, then the myMother object would "inherit" the "age"  property. So... with reference to your last comment, I must be misunderstanding the "inherit" thing.

I believe, but I am not certain, that inheritance refers only to properties and methods passed from one class (prototype function) to another.  An object is not a class.  It does not inherit (using the term loosely in this context) in the same way that a class does.

Making more practical sense of what I just stated.  The object allows you to perform certain operations on the constructor function from which it was created, but does not allow you to mess with any of the properties or methods that the constructor function inherited from the Object class that allowed you to produce the constructor function.  

Surely delete is an inherited method from the Object class that allows you to perform certain operations on the class from which your object was created.

Link to comment
Share on other sites

In effect, you have the following scenario:
 

Object prototype => Person prototype => Person object                   

Inheritance:  The Person prototype inherits from the Object prototype

No inheritance:  The Person object has access to what is inherited by the Person prototype from the Object prototype and part of that inheritance includes use of the DELETE keyword on the properties of the prototype from which the object was created.

Link to comment
Share on other sites

Just watched a video on youtube on inheritance by Nathan Wall (might be of interest to someone who struggling like me) and together with the replies I've had here......, I get the inheritance thing now. Thanks to all that have replied to this post.  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...