0

The main.js has the following code

    APP.open = (function ($) {
    return {
      people: (function() {
        return {

          gender: function() {},

          age: function() {}
         }
       })()
     };
    }(jQuery));

and exteral.js has this:

    APP.open = (function ($) {
    return {
      people: (function() {
        return {
          race: function() {}
         }
       })()
     };
    }(jQuery));

exteral.js is loaded last so is overwriting main.js.

My question is, how should I write exteral.js in order to stop overwriting people method. I would like to extend APP.open.people method.

2
  • like as in you want the method in external to call the super (main.js) people as well as append its own code? Commented Apr 21, 2015 at 19:01
  • This is a good reading to learn how you should write your code to mimic inheritance in JS. github.com/getify/You-Dont-Know-JS/blob/master/… Commented Apr 21, 2015 at 19:04

3 Answers 3

1

Add the specific method instead of replacing the entire APP.open namespace:

APP.open.people.race = function() {};

If you don't know which namespaces exist, add them first:

if (!'open' in APP) APP.open = {};
if (!'people' in App.open) App.open.people = {};
Sign up to request clarification or add additional context in comments.

Comments

1
  1. You need to stop appending to the jQuery as it is a bad programming habbit
  2. You need to define your own space and use it for your code:

So the code will look like:

var LS = LS || {};
LS.open = function () {...}

and a call to it:

LS.open();

You can find more about JS modules here.

Comments

0

In your external.js you can try something like this:

var APP = APP || {}; 
APP.open.people = function(){return {race:function(){}}};

Comments

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.