1

The code is here:

Father.js
(function(){
function father(){
};
father.prototype.init = function(){
 console.log('father');
}
})()
Child.js
(function(){
function child(){
}
child.prototype.init = function(){
 console.log('child');
}
var father = new father();
 })()

I have 2 questions: How can I call father object or child object between script tag or any third javascript files that I create ? Second: how can I call the father object inside child class. I am new to JS and have some problems with OOP in javascript. Thank you very much for your help

asked Jul 13, 2013 at 2:55
2
  • 3
    Like var Father = (function(){...; return Father }()); Commented Jul 13, 2013 at 3:10
  • @elclanrs: You should post that as an answer. Commented Jul 13, 2013 at 3:12

3 Answers 3

4

You should assign the result of the anonymous function to a variable that way you can use it without leaking what's inside of the IIFE (Immediately Invoked Function Expression), thus encapsulating everything but the constructor:

var Father = (function(){
 // Private stuff...
 function Father(){}
 Father.prototype.something = function(){};
 return Father; // Public constructor
}());

Now you can use Father in your Child class, or better yet, use the same pattern but pass the parent class as parameter to the IIFE:

var Child = (function(_parent){
 function Child() {
 _parent.apply(this, arguments); // inherit properties from Parent class
 }
 Child.prototype = Object.create(_parent.prototype); // inherit Parent prototype
 Child.prototype.something = function(){};
 return Child;
}(Father));
answered Jul 13, 2013 at 3:23
Sign up to request clarification or add additional context in comments.

2 Comments

That is what exactly I am looking for. Thank you very much for all the answers.
Note that Object.create isn't supported in older browsers (IE 8) and has Child.prototype.constructor point to the wrong object. This could be of value if you plan to use this.constructor at some point in Child intances.
0

Answer to question one is you define father and child in the global scope:

function father(){
};
father.prototype.init = function(){
 console.log('father');
}
function child(){
}
child.prototype.init = function(){
 console.log('child');
}
// can't name your var father because it'll overwrite the father function
var dad = new father();

You can use namespacing to restrict the amounts of variables in the global scope:

in father.js:

var myApp=myApp || {};
myApp.father=...

in child.js:

var myApp=myApp || {};
myApp.child=...
var dad = new myApp.father();

To call father object in child you can do father.call(this); making all the father properies in the father function defined as this.someprop=... part of the just created child. If you just want to access the father instance named dad (see code above) then you can do dad.init()

More on inheritance and prototype here:

Prototypical inheritance - writing up

answered Jul 13, 2013 at 3:13

2 Comments

If I do that, how can I include jQuery inside class ? Thank you very much for your answer
You add jQuery as the first file and you can use $ in any other included files.
0

I've set up a fiddle : http://jsfiddle.net/J75Zz/

It doesn't matter on how many .js files you "distribute" your code, it's just "one code block", which get's executed (well, almost ...).

You should name Objects with Capitals, there's already a clash between object father and variable father.

answered Jul 13, 2013 at 3:20

2 Comments

Note that Child doesn't overload Father's init function en.wikipedia.org/wiki/Method_overloading and it doesn't technically override it either because Child doesn't have Father as prototype (didn't inherit init from Father in the first place)
From my fiddle: Yes, but is the overloading requested by OP? Q was Second: how can I call the father object inside child class and I understand that as a simple instantiation of object child within father? Answer to that was already given.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.