下面這段代碼,為何控制臺輸出的B型別物件的實體b,卻被認為是A型別?求解答

<!DOCTYPE html>
<html lang = "zh-cmn-Hans">
<head>
<title>繼承的四種模式</title>
<meta charset = "utf-8">
</head>
<body>
<script type = "text/javascript">
function A () {}
var a = new A();
B.prototype = a;
function B () {}
var b = new B();
C.prototype = b;
function C () {}
console.log( b ); //A{}為何控制臺輸出的b被認為是A型別?
</script>
</body>
</html>
uj5u.com熱心網友回復:
因為fn B的原型物件指向了A呀!如果你想讓b的輸出是B{ },可以使用如下代碼,修改B的原型,指向A的原型,并規定其consructor
B.prototype = Object.create(A.prototype, { constructor: { value: A } });
uj5u.com熱心網友回復:
謝謝,果然是這樣的!轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/43854.html
標籤:JavaScript
上一篇:Git,求大佬幫忙理一下分支
