52
console.log(Function.prototype.__proto__);
console.log(Function.__proto__);
console.log(Object.__proto__);
console.log(Object.prototype.__proto__);
[Object: null prototype] {}
Function
Function
null
53
function Post(title) {
this.title = title;
}
console.log(Post.constructor);
console.log(Post.prototype);
console.log(Post.prototype.constructor);
console.log(Post.prototype.__proto__);
console.log(Post.__proto__);
var post = new Post('mianshiya');
console.log(post.constructor);
console.log(post.prototype);
console.log(post.__proto__);
console.log(post.__proto__.__proto__);
console.log(post.__proto__.__proto__.__proto__);
console.log(post.__proto__.__proto__.__proto__.__proto__);
console.log(post.__proto__.__proto__.__proto__.__proto__.__proto__);
[Function: Function]
{}
[Function: Post]
[Object: null prototype] {}
[Function]
[Function: Post]
undefined
{}
[Object: null prototype] {}
null
TypeError: Cannot read property 'proto' of null
54
function FuncObj() {
print = function () {
console.log(1);
};
return this;
}
FuncObj.print = function () {
console.log(2);
};
FuncObj.prototype.print = function () {
console.log(3);
};
var print = function () {
console.log(4);
};
function print() {
console.log(5);
}
FuncObj.print();
print();
FuncObj().print();
print();
new FuncObj.print();
new FuncObj().print();
new new FuncObj().print();
55
var FuncObj = function () {};
Object.prototype.foo = function () {
console.log('foo');
};
Function.prototype.bar = function () {
console.log('bar');
};
FuncObj.foo();
FuncObj.bar();
var f = new FuncObj();
f.foo();
f.bar();
foo
bar
foo
TypeError: f.bar is not a function
56
function FuncObj(){
FuncObj.func = function(){
console.log('A');
}
this.func = function(){
console.log('B')
}
}
FuncObj.prototype.func = function(){
console.log('C');
}
FuncObj.func = function(){
console.log('D');
}
FuncObj.func();
let obj = new FuncObj();
obj.func();
FuncObj.func();
57
function CoolBoy() {
this.name = 'yupi';
}
CoolBoy.prototype.rap = () => {
console.log('i am a rapper');
};
const boy = new CoolBoy();
console.log(
CoolBoy.prototype.constructor === CoolBoy &&
boy.constructor === CoolBoy &&
boy instanceof CoolBoy,
);
58
var Obj1 = { value: 10 };
var Obj2 = function () {
this.value = 20;
};
var Obj3 = function () {
var value = 30;
};
Obj2.prototype = Obj1;
Obj3.prototype = Obj1;
var b = new Obj2();
var c = new Obj3();
Obj1.value++;
console.log(b.value);
console.log(c.value);
59
function Obj1() {}
function Obj2(value) {
this.value = value;
}
function Obj3(value) {
if (value) {
this.value = value;
}
}
Obj1.prototype.value = 1;
Obj2.prototype.value = 1;
Obj3.prototype.value = 1;
console.log(new Obj1().value);
console.log(new Obj2().value);
console.log(new Obj3(666).value);
60
function Father() {
this.a = 1;
this.b = [1, 2, this.a];
this.c = { field: 5 };
this.print = function () {
console.log(this.a, this.b, this.c.field);
};
}
function Son() {
this.a = 2;
this.update = function () {
this.b.push(this.a);
this.a = this.b.length;
this.c.field = this.a++;
};
}
Son.prototype = new Father();
var father = new Father();
var son1 = new Son();
var son2 = new Son();
son1.a = 11;
son2.a = 12;
father.print();
son1.print();
son2.print();
son1.update();
son2.update();
father.print();
son1.print();
son2.print();
1 [ 1, 2, 1 ] 5
11 [ 1, 2, 1 ] 5
12 [ 1, 2, 1 ] 5
1 [ 1, 2, 1 ] 5
5 [ 1, 2, 1, 11, 12 ] 5
6 [ 1, 2, 1, 11, 12 ] 5
61
function Father(){
this.value = true;
}
Father.prototype.getValue = function(){
return this.value;
};
function Son(){
this.subValue = false;
}
Son.prototype = new Father();
Son.prototype.getSubValue = function () {
return this.subValue;
};
var son = new Son();
console.log(son.getValue());