HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Creating Inheritance hierarchy using function constructor

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
creatinghierarchyfunctioninheritanceconstructorusing

Problem

Problem statement



-
You need to create the Animal base class having four fields:


a. name


b. sound


c. owner


d. favFood

-
You need to create a derived class Cat such that every instance of Cat inherits instance members of Animal. In addition, Cat adds a new instance member mode.

-
Animal and Cat class should enforce encapsulation amidst construction and modification of objects.


Solution

```


OOP assignment




function Animal() {
this.name = "No Name";
this.sound = "Grrr";
this.owner = "Homeless";
this.favFood = "Anything";
/ Animal.__proto__ points to Function.prototype /
}

/ Animal.prototype.__proto__ points to Object.prototype /
Animal.prototype.setOwner = function(newOwner){
if (typeof newOwner != 'undefined'){
this.owner = newOwner;
}
else
{
document.write("Please enter a valid owner name" + "");
}
}

Animal.prototype.getOwner = function(){
return this.owner;
}

Animal.prototype.setName = function(newName){
if (typeof newName != 'undefined'){
this.name = newName;
}
else{
document.write("Please enter a valid animal name" + "");
}
}

Animal.prototype.getName = function(){
return this.name;
}

Animal.prototype.setNoise = function(newNoise){
if (typeof newNoise != 'undefined'){
this.sound = newNoise;
}
else{
document.write("Please enter a valid animal sound" + "");
}
}

Animal.prototype.getNoise = function(){
return this.sound;
}

/* dog.__proto__ points to Animal.prototype

Solution

Does the __proto__ hierarchy and contents of Cat.prototype looks good?

It looks reasonable. You can test via sophie instanceof Animal or Object.getPrototypeOf(sophie) depending on need.


Did Animal and Cat class enforce encapsulation?

No. We can still do sophie.name='foo' via the objects fields. For instance i could set sophie.name = function() { alert('hello, world'); };

Encapsulation would look more like below whereby we control how and what happens to the values of those fields:

function Animal(name, sound, owner, favFood) {
    var _name = name || "animal";
    var _sound = sound || "roar";
    var _owner = owner || "Homeless";
    var _favFood = favFood || "Jelly beans";

    Object.defineProperties(this, {
        'name': {
            get: function () {
                return _name;
            },
            set: function (name) {
                _name = name;
            }
        },
        'owner': {
            get: function () {
                return _owner;
            },
            set: function (ownerName) {
                _owner = ownerName;
            }
        }
    });
}


There are additional configuration options like leaving off a setter for read-only access, plus writable, configurable and enumerable parameters which can help you completely lock the object down.


What is the problem, when I say, Cat.prototype = new Animal() instead
of Cat.prototype = Object.create(Animal.prototype);?

Cat.prototype = new Animal() invokes the function and therefore executes all its statements so it potentially has more/unintended side effects.

Code Snippets

function Animal(name, sound, owner, favFood) {
    var _name = name || "animal";
    var _sound = sound || "roar";
    var _owner = owner || "Homeless";
    var _favFood = favFood || "Jelly beans";

    Object.defineProperties(this, {
        'name': {
            get: function () {
                return _name;
            },
            set: function (name) {
                _name = name;
            }
        },
        'owner': {
            get: function () {
                return _owner;
            },
            set: function (ownerName) {
                _owner = ownerName;
            }
        }
    });
}

Context

StackExchange Code Review Q#114251, answer score: 5

Revisions (0)

No revisions yet.