1

there is module which was implemented in specific JS file in my project like the following example

define("Company", ["exports", "Employee"], function(Employee) {
    function Company(name) {
        this.name = name;
        this.employees = [];
    };
    Company.prototype.addEmployee = function(name) {
        var employee = new Employee.Employee(name);
        this.employees.push(employee);
        employee.company = this;
    };
    exports.Company = Company;
});

Now In different JS file (new module) I want to extend addEmployee method, for example I want to add address data to it,is it possible??? if yes how should I do that ? example will be very helpful!

Thanks in advance!

9
  • Why would you want to do it in a different module? It get's very difficult from a maintenance point of view when you have different definitions in different places. Commented Mar 17, 2015 at 9:17
  • @Magrangs-Thanks but there is module which have a basic functionality for specific use-case(this file contain almost 2000 lines of code... :)not mine... ) Now I want to use some of the method there which do some basic functionality and extend it to support new version,my question is how should I do it right and clean... Commented Mar 17, 2015 at 9:23
  • The issue is you cannot just replace the functionality as it will affect everything that uses the company module. Commented Mar 17, 2015 at 9:44
  • @Magrangs- what do you mean will affect everything? there is other solution which you recommended to use which is elegant ...? Commented Mar 17, 2015 at 9:47
  • 1
    check out the answer by @Zemljoradnik (he answered before I had chance :)) Commented Mar 17, 2015 at 10:19

2 Answers 2

1

If you want to change this method for one instance only what you can do is create a new instance of Company in your module, override addEmployee method on your instance by adding the code of your own, and than call the original method from prototype:

// create a new instance of Company
var myCompany = new Company('myCompany');
// add method 'addEmployee' to your instance, it will override original method
// you can, of course, add more arguments if you wish
myCompany.addEmployee = function(name) {
    // your code here
    // and than call the original method settings it's 'this' to 'this' of your instance, 
    // and providing appropriate arguments
    Company.prototype.addEmployee.call(this, name);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks i try it and it doesnt work for me jsfiddle.net/NssGv/96 do i miss something?
You are trying to use require.js in jsfiddle, and I'm not sure if that's even possible. You're creating a new Employee, but there's no definition of Employee. new company has to be new Company.
@Zemljoradnik-lets assume that I overcome on the require issue,what Im doing wrong in the jsFiddle? can you please fix it and share ?,Thanks in advance!
For example, if you want to add the address to the employee: jsfiddle.net/v3xeekcd.
0

Does something like this work?

//required as Company
(function(){
  var addEmployee = Company.prototype.addEmployee;
  Company.prototype.addEmployee = function(name, address){
    //assuming the original addEmployee returns the created instance
    var emp = addEmployee.call(this,name);
    //do something with address
    emp.address=address;
    return emp;//also return the created employee so it'll behave the same as the original
  }
}())

2 Comments

Thanks ! do you mean something like this,jsfiddle.net/NssGv/94 .I try it and it doesnt work...do I miss something ?
@JhonDree your original addEmployee does not return anything so you can either return employee in the original or use this.employees[this.employees.length-1] to set the address

Your Answer

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