1

I have the following code and have came across the call and prototype.constructor methods but don't have enough knowledge to make them work properly. Can some one fill in the knowledge I am missing. Here an Administrator is a User.

    function User(user) {
      this.id = user.id;
      this.email = user.email;
      this.firstname = user.firstname;
      this.lastname = user.lastname;
      this.age = user.age;
    }       

    User.prototype.fullName = function(){
        return this.firstname + ' ' + this.lastname
    }

    function Admin(admin){
        this.writer = admin.writer;
        this.editor = admin.editor;
        this.publisher = admin.publisher;
        //User.call(this);
    }

    Admin.prototype.fullAccess = function(){
        return (this.writer && this.editor && this.publisher);
    }

    //Admin.prototype = new User();
    //Admin.prototype.constructor = Admin;

    var user1 = new User({
        'id': 1,
        'email': '[email protected]', 
        'firstname': 'Stephen',
        'lastname': 'Brown',
        'age': 44
    });

    var user2 = new User({
        'id': 2,
        'email': '[email protected]', 
        'firstname': 'John',
        'lastname': 'Doe',
        'age': 25
    });

    var admin1 = new Admin({
        'writer': true,
        'editor': true, 
        'publisher': true,
    });

    var admin2 = new Admin({
        'writer': true,
        'editor': true, 
        'publisher': false,
    });     
1

1 Answer 1

2

You were almost there, it works with a few simple changes:

  1. Uncomment your commented lines
  2. Change User.call(this); to User.call(this, admin);. This will pass the parameters passed to the Admin constructor ahead to the "super" constructor.
  3. Change Admin.prototype = new User(); to Admin.prototype = new User({}); (pass an empty object, otherwise the User constructor will throw an error for trying to access properties of undefined). Or just use Admin.prototype = Object.create(User.prototype); (polyfill required for IE<=8).

http://jsfiddle.net/P6ADX/

Sign up to request clarification or add additional context in comments.

5 Comments

please check out how I'm eliminating the problem with the need to pass 'defaults' to create a prototype object: metadea.de/V
Thats great, its nearly working. Only thing is I'm getting the error - TypeError: admin1.fullAccess is not a function when I try accessing it.
Switching the lines around seems to have fixed it.... Admin.prototype = new User({}); Admin.prototype.constructor = Admin; Admin.prototype.fullAccess = function(){ return (this.writer && this.editor && this.publisher); }
@StephenBrown Glad you figured that out, I'm too late! :-)
@metadings I didn't have enough time to understand your library in depth, but I'll keep it bookmarked and come back to it later. Looks like you put a lot of effort there - although my personal js coding style is to usually avoid classical inheritance patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.