请问这两种原型继承方式有啥区别:
1.function father(){......};
function child(){.....};
child.prototype=father.prototype;
2.
function father(){......};
function child(){.....};
function F(){};
F.prototype=father.prototype;
child.prototype=new F();
3.function father(){......};
function child(){.....};
child.prototype=new father;
请问这三种方式各有什么优劣呢??为什么第二个要新添一个F呢?
请指教!~谢谢~~~
1.function father(){......};
function child(){.....};
child.prototype=father.prototype;
2.
function father(){......};
function child(){.....};
function F(){};
F.prototype=father.prototype;
child.prototype=new F();
3.function father(){......};
function child(){.....};
child.prototype=new father;
请问这三种方式各有什么优劣呢??为什么第二个要新添一个F呢?
请指教!~谢谢~~~
http://blog.sina.com.cn/s/blog_6e52b8600100m7z7.html
child.prototype指向类似child的一个实例,对prototype赋值就是给child实例添加属性或者方法,child实例具有的属性和方法则child的其他实例也是具有的。所以我觉得
child.prototype=new father;
child.prototype=father.prototype;
大体上相同,只是后者father上不是通过prototype设置的属性或者方法,child没有继承。新添一个F,就是说明F仅仅继承prototype设置的属性或者方法,即构造了一个中间类,这个类组合prototype设置的属性或者方法,child则对F这个中间类进行继承。
<script type="text/javascript">
function father(){this.c="attribute";this.b=function(){}};
father.prototype.a = function(){};function child(){};
child.prototype=father.prototype;
var child = new child();
alert(child.b);
var father = new father();
alert(father.b);
</script>