Javascript Privilege Function Inheritance
NickName:ossys Ask DateTime:2015-07-30T08:18:47

Javascript Privilege Function Inheritance

I have spent the past few weeks doing a ton of research on Javascript inheritance. Firstly, I want to say that I am NOT trying to implement class-based inheritance in Javascript, however, I am trying to mimic inheritance CORRECTLY using Javascript's prototypal nature. This may be beating a dead horse, but here goes my question:

I have two simple functions:

function Animal(arg) {
    var a = arg;

    //privileged function has access to private variable
    this.sayHi = function(name) {
        // can access a here if needed
        console.log('Hi: ' + name);
    }
}

and:

function Person(arg) {
    this.sayHi = function(name) {
        super.sayHi(name + '!!!');
    };
}

Person.inheritsFrom(Animal);

The following would be the expected result:

var animal = new Animal('cow');
animal.sayHi('John'); // Hi: John

var person = new Person('adult');
person.sayHi('Fred'); // Hi: Fred!!!

The "inheritsFrom()" method that I'm using for inheritance looks like:

Function.prototype.inheritsFrom = function(parent) {
    if(parent.constructor == Function) { //Normal Inheritance
        this.prototype = new parent;
        this.prototype.constructor = this;
        this.prototype.parent = parent.prototype;
    } else  { //Pure Virtual Inheritance
        this.prototype = parent;
        this.prototype.constructor = this;
        this.prototype.parent = parent;
    }
    return this;
}

These are some of my references: http://phrogz.net/JS/classes/OOPinJS2.html http://www.crockford.com/javascript/inheritance.html https://medium.com/javascript-scene/common-misconceptions-about-inheritance-in-javascript-d5d9bab29b0a

Lots of good info, still can't figure out how to inherit "privileged" methods as shown above. I'm able to call the privileged method of the parent class. Please see the following JS Fiddle: https://jsfiddle.net/jxjk5hm9/

Any guidance on inheriting privileged methods is greatly appreciated, thanks!

Copyright Notice:Content Author:「ossys」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31713291/javascript-privilege-function-inheritance

More about “Javascript Privilege Function Inheritance” related questions

Javascript Privilege Function Inheritance

I have spent the past few weeks doing a ton of research on Javascript inheritance. Firstly, I want to say that I am NOT trying to implement class-based inheritance in Javascript, however, I am tryi...

Show Detail

Principle of least privilege and the const keyword

Classes are about inheritance and composition. Is using the const keyword related to the principle of least privilege? I understand inheritance and composition and I understand them but what about

Show Detail

Javascript inheritance

I'm trying a few different approaches to Javascript inheritance at the moment. I have the following code: ('borrowed' from http://www.kevlindev.com/tutorials/javascript/inheritance/index.htm) KV...

Show Detail

JavaScript inheritance

Douglas Crockford seems to like the following inheritance approach: if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; ...

Show Detail

Javascript inheritance implementation question

In his sitepoint article about javascript inheritance, Harry Fuecks explains a way of implementing inheritance as follows: function copyPrototype(descendant, parent) { var sConstructor = p...

Show Detail

Javascript inheritance misbehaviour

I have some js code here link deleted If you open your js console and you'll run this code snipet var r = new TempPopupForm("xxx"); r.create(); an error will appear TypeError: this.init is not a

Show Detail

Javascript OOP - inheritance, prototyping, callback function

I'm trying to use OOP in Javascript with inheritance, prototyping and callback functions. Would you please have a look at my JSfiddel http://jsfiddle.net/Charissima/5g6GV/. The first problem is sol...

Show Detail

Inheritance in JavaScript

I'm coding a JavaScript client to a REST JSON API. Since I don't want it to depend on any other libraries I'm doing it with vanilla javascript. Everything have been working great, but I'm having

Show Detail

Understanding prototypal inheritance javascript

This is sort of a follow up of from this question: Python like inheritance for JavaScript But I phrased it wrong and made it seem like I wanted classical inheritance in JavaScript when I just want...

Show Detail

Inheritance in Javascript function error

I apply inheritance in JavaScript in following way: var employee = function(name) { this.name = name; } employee.prototype.getName = function() { return this.name; } va

Show Detail