继承:
只要某个对象使用了另一个对象具有,而自身不具有的功能,就叫继承
继承,是面向对象的三大特点。
封装,继承,多态
f.proto === Fn.prototype
原型,是父级
读取规则:当对象在找方法或属性时,自己没有,会找父级,父级也没有,继续向上找父级,直到找到顶层对象,还没有,抛出undefined;如果有一层找到了,就停止向上,同时执行;多层同时存在,就近原则。
function Parent(n){
this.num = n;
this.show = function(){
console.log(this.num);
}
}
Parent.prototype.init = function(){
console.log(“hello”)
}
function Child(n){
// 想在C中使用P的方法和属性
// P的方法和属性加给谁了?this
// P中的this是将来new出来的p
// C中的this是将来new出来的c
// c要执行show,在C中的this得有show
// 没有,P的this有show
// 手术:强行将P的this指向C的this
// 哪里能拿到C的this?在C中
// 手术得在C中做
Parent.call(this,n);
// Parent.apply(this,[n]);
// Parent.bind(this,n)();
}
var p = new Parent(3.1415);
p.show();
p.init()
var c = new Child(520);
c.show();
c.init()