• 首页
  • little笔记小助手
  • 聊天
  • 工具
  • 登录/注册

代码分析3-prototype

用户头像
新鲜噩梦
little笔记全栈作者
创建于:2025-06-20 13:55:50字数:4070
52
js
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
js
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
js
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();
2
4
1
1
2
3
3
55
js
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
js
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();

D
B
A
57
js
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,
);


true
58
js
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);

20
11
59
js
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);

1
undefined
666
60
js
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
js
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());

true
最后编辑于:2026-01-11 09:11:46
©著作权归作者所有,转载或内容合作请联系作者。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,little笔记系信息发布平台,仅提供信息存储服务。