非构造函数的继承的详细解释

Javascript面向对象编程(三):非构造函数的继承这里,可以看到详细的说明。

我只是将其中的例子做成html文件,便于调试罢了。

<html>
<head>
<script type="text/javascript">
function extendCopy(p) {
	var c = {};
	
	for (var i in p) {
		c[i] = p[i];
	}
	c.uber = p;
	
	return c;
}

function deepCopy(p, c) {
	var c = c || {};
	
	for (var i in p) {
		if (typeof p[i] === 'object') {
			c[i] = (p[i].constructor === Array) ? [] : {};
			deepCopy(p[i], c[i]);
		} else {
			c[i] = p[i];
		}
	}
	
	return c;
}

function object(o) {
	function F() {}
	F.prototype = o;
	
	return new F();
}

var Chinese = {
	nation:'中国'
};
var Doctor = object(Chinese);
Doctor.career = '医生';
alert(Doctor.nation);

Chinese.birthPlaces = ['北京','上海','香港'];
var Doctor2 = extendCopy(Chinese);
//change child
Doctor2.birthPlaces.push('厦门');
alert(Doctor2.birthPlaces);
alert(Chinese.birthPlaces);

var Doctor3 = deepCopy(Chinese);
//change child
Doctor3.birthPlaces.push('厦门2');
alert(Doctor3.birthPlaces);
alert(Chinese.birthPlaces);
</script>
</head>
<body>
	Test
</body>
</html>
下面时从web developer debugger上的截图。

1.通过继承来获得Chinese的nation和birthPlaces属性。

2. 这里时浅copy,所有更改Doctor会同时更改Chinese

3. 这里时深copy,所有Doctor和Chinese互不影响。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值