关于 Js 原型链继承的一些解惑

<script>
			function Father() {
				this.property = true;
			}

			Father.prototype.getSuperValue = function() {
				return this.property;
			}

			function Son() {
				this.subproperty = false;
			}
			Son.prototype = new Father();
			Son.prototype.getSonValue = function() {
				return this.subproperty;
			}
			
			var instance =new Son();
			alert(instance.getSonValue());
		</script>

Father 函数:

为 Father 的函数原型增加了一个方法 getSuperValue 返回自身的 property 属性。

Son 函数:

Son 的 prototype 指针指向了一个 Father 的实例。由于 Son 的 prototype 默认指向自身的函数原型,这里更改了它的指向。

由于 new 了一个 Father 对象,这个对象的自身的原型对象是 Father,这也就是意味着 Son 可以访问 Father 的各种方法。

var instance =new Son();

这一句创建了一个 Son 的实例。

Son
subproperty 对象自带属性
getSonValue 通过 prototype 访问到
getSuperValue 通过 prototype 访问到
property 对象自带属性

这里解释一下

var fa = new Father();无法访问子类属性的原因。

Son.prototype = new Father();
			Son.prototype.getSonValue = function() {
				return this.subproperty;
			}

new Father()这条语句开辟了一个新的内存,它指向了 Father 的原型。但是Son.prototype.getSonValue 是在新开辟的内存上的操作,没有操作到 Father 的原型对象。如果要操作到 Father 的原型需要:

Son.prototype.__proto__.getSonValue =function(){
				return this.property;
			};