以下是原型链测试题
测试题1,请思考打印的值为多少?
<script>
function Person(){}
Person.prototype.n=1
var p1=new Person()
Person.prototype={
n:2,
m:3
}
var p2=new Person()
console.log(p1.n,p1.m,p2.n,p2.m)
</script>
测试题2 请思考如何打印?
<script>
function F() {}
Object.prototype.a = function () {
console.log("a()")
};
Function.prototype.b = function () {
console.log("b()")
}
var f = new F()
f.a()
f.b()
F.a()
F.b()
</script>
下面是分析图和结果
测试题1
打印结果
1 undefined 2 3
测试题2
打印结果
a() 、f.b is not a function、a() 、b()