4

I am wondering whats the difference between inheritFrom and the prototype when defining inheritance in Javascript.

function classA{}
classA.name="abc";
classA.functionName=function(){
alert("Function Name Alert");
}
function classB{ }

Whats the difference in the below codes?

classB.prototype=classA();

and

classB.prototype.inheritFrom(classA);
3
  • 2
    Where does inheritFrom come from ? Commented Sep 10, 2011 at 17:36
  • Searching for inhertFrom, I've found this: java2s.com/Tutorial/JavaScript/0500__Object-Oriented/… Commented Sep 10, 2011 at 17:39
  • it comes from zinherit library... Commented Sep 10, 2011 at 17:39

1 Answer 1

6

B.prototype.inheritFrom(A) is not standard JavaScript, whereas B.prototype = new A is standard JavaScript. I suggest learning the ins and outs of JavaScript and embracing the prototype. You'll be better off for knowing it. It's really not too difficult:

function A(){}
function B(){}
B.prototype = new A;
b = new B;
console.log(b instanceof B, b instanceof A);
//-> true, true
Sign up to request clarification or add additional context in comments.

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.