Function 和 Object 原型链关系

转自:https://blog.csdn.net/weixin_41784648/article/details/108252962

一些关于原型链的题目:

1. 让 Foo 继承 Bar,求 Foo.__proto__、Foo.prototype.__proto__ 以及 Function.__proto__

Function.prototype.a = () => {
  console.log('a');
};
Bar.prototype.b = () => {
  console.log('b');
};
function Foo() {

}
function Bar() {

}
Foo.prototype = new Bar();
console.log(Foo.__proto__);  // Function.prototype
console.log(Foo.prototype.__proto__);  // Bar.prototype
console.log(Function.__proto__ === Function.prototype);
// Function 比较特殊,它的__proto__【隐式原型】就是它的prototype【显式原型】

2. 仍然是关于 Function、Object 的关系题目:

Function.prototype.a = () => {
  console.log(1);
};
Object.prototype.b = () => {
  console.log(2);
};
function A() {
}
const obj = new A();

obj.a(); // TypeError:obj.a is not a function.
obj.b(); // 2
A.a(); // 1
A.b(); // 2

console.log(A instanceof Function); // true
console.log(obj instanceof Function); // false
console.log(obj instanceof Object); // true

 

posted @ 2021-10-05 19:23  TwinkleG  Views(57)  Comments(0)    收藏  举报