Object Inheritance in JavaScript

前端 未结 2 1206
情话喂你
情话喂你 2020-12-16 08:00

My question is regarding a child object maintaining the prototype chain of its parent object.

In John Resig\'s Advanced Javascript slides (http://ejohn.org/apps/lear

2条回答
  •  一个人的身影
    2020-12-16 08:50

    If you don't like the way prototyping works in JavaScript in order to achieve what you need, I'd suggest taking a look at this: https://github.com/haroldiedema/joii

    It basically allows you to do the following (and more):

    var Employee = new Class(function() {
        this.name = 'Unknown Employee';
        this.role = 'Employee';
    });
    
    var Manager = new Class({ extends: Employee }, function()
    {
        // Overwrite the value of 'role'.
        this.role = 'Manager';
    
        // Class constructor to apply the given 'name' value.
        this.__construct = function(name) {
            this.name = name;
        }
    });
    
    var myManager = new Manager("John Smith");
    console.log( myManager.name ); // John Smith
    console.log( myManager.role ); // Manager
    

提交回复
热议问题