When bar.prototype = new Foo ();
$(function () { varTest =NewBar (); Console.log (test);//BarTest.method ();//Method LogConsole.log (Test.foo);//Hello WorldConsole.log (Test.value);// the varFootest =NewFoo (); Console.log (footest);//FooConsole.log (Footest.foo);//undefinedConsole.log (Footest.value);// the}); functionFoo () { This. value=42; } Foo.prototype={method:function() {Console.log (' method log ')}};functionBar () {}bar.prototype=NewFoo (); Bar.prototype.foo= ' Hello world '; Bar.prototype.constructor=bar;
When Bar.prototype=foo.prototype;
$(function () { varTest =NewBar (); Console.log (test);//BarTest.method ();//Method LogConsole.log (Test.foo);//Hello WorldConsole.log (Test.value);//undefined varFootest =NewFoo (); Console.log (footest);//FooConsole.log (Footest.foo);//Hello WorldConsole.log (Footest.value);// the}); functionFoo () { This. value=42; } Foo.prototype={method:function() {Console.log (' method log ')}};functionBar () {}bar.prototype=Foo.prototype; Bar.prototype.foo= ' Hello world '; Bar.prototype.constructor=bar;
Principle: Supplement.
JavaScript Tips: Prototype chain prototype