一、
var A = function() {};
A.prototype.n = 1;
var b = new A();
A.prototype = {
n: 2,
m: 3
}
var c = new A();
console.log(b.n);
console.log(b.m);
console.log(c.n);
console.log(c.m);
//答案
b.n -> 1
b.m -> undefined;
c.n -> 2;
c.m -> 3;
二、
var F = function() {};
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();
//答案
f.a() -> a
f.b() -> f.b is not a function
F.a() -> a
F.b() -> b
三、
function Person(name) {
this.name = name
}
let p = new Person('Tom');
问题1:1. p.__proto__等于什么?
问题2:Person.__proto__等于什么?
答案1:Person.prototype
答案2:Function.prototype
四、
var foo = {}
var F = function(){}
Object.prototype.a = 'value a';
Function.prototype.b = 'value b';
console.log(foo.a);
console.log(foo.b);
console.log(F.a);
console.log(F.b);
//答案
foo.a => value a
foo.b => undefined
F.a => value a
F.b => value b
Object.prototype.__proto__ //null
Function.prototype.__proto__ //Object.prototype
Object.__proto__ //Function.prototype