Fork me on GitHub

javascript再学习之prototype

Function中

有prototype 和__proto__

第一种情况 对象中有say

对象

1
2
3
4
5
6
7
8
function Person(){
this.say = function(){
console.log("Hello");
};
}
var person = new Person();
person.say(); //hello
debugger;

ES6

1
2
3
4
5
6
7
8
9
10
11
12
class Person {
constructor() {
this.say = function() {
console.log("hello");
};
}
}

const person = new Person();
const p2 = new Person();
console.log(person.say === p2.say);// false
debugger;

第二种情况 原型上有say

原型

p1.say == person.say 肯定是true

1
2
3
4
5
6
7
8
9
function Person {  
}
Person.prototype.say = function(){
console.log("hello");
}
const person = new Person();
const p2 = new Person();
person.say();//hello
debugger;

prototype实际上也是一个对象,然后constructor就是function Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Person  = function () {
console.log("person");
}
Person.prototype ={
constructor: Person,
say : function(){
console.log("hello");
}
}
const person = new Person();//person
const p2 = new Person();//person
person.say();//hello
person.say == p2.say; //true
debugger;

ES6

1
2
3
4
5
6
7
8
9
class Person {
say() {
console.log("hello");
}
}
const person = new Person();
const p2 = new Person();
person.say();
debugger;

第三种情况 两个都有say,执行哪一个?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var Person  = function () {
console.log("person");
this.say = function(){
console.log("hello2");
}
}
Person.prototype ={
constructor: Person,
say : function(){
console.log("hello");
}
}
const person = new Person();//person
const p2 = new Person();//person
person.say();//hello2
person.say == p2.say; //false
debugger;

person.say 执行最近的一个,因为对象中有,所以直接执行hello2,如果没有的话就找prototype中的方法,如果没有这个方法,则出错。

Person 方法的__proto__

Person.__proto__ === Function.prototype

这里实际上Person已经作为了Function的一个实例,对应到Person上来说,对象的__proto__和构造方法的prototype 是相等的

即 person.__proto__ === Person.prototype

Object中

只有 __proto__

如果作为一个单纯的对象来说,是只有__proto__

所以对于Object来说,实际上是Function的一个实例 ,那么 Object.__proto__ === Function.prototype;

同时Object 也是一个方法,任何一个 var a ={},这样的话,a.__proto__ === Object.prototype;

方法的prototype 是对象吧,那么Function.prototype就有 __proto__, 所以下面一个就说的通了,

Function.prototype.__proto__ ===Object.prototype;

那么Object.prototype 是对象,那么它的__proto__是什么呢? null

下面这个怎么解释?

Function.__proto__ === Function.prototype

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


var A = function(){}; // A是一个方法,当然也是个对象
var a = new A(); // a是一个由A创造出的对象,不是方法

//看一看对象非方法的行为
console.log(a.__proto__); //object
console.log(a.prototype); //undefined, 对象没有

//看一看方法的行为,方法也是对象哦

console.log(A.__proto__); //function
console.log(A.prototype); //object



console.log(a.__proto__ === A.prototype); //true
console.log(a.prototype === undefined); //true

console.log(A.__proto__ === Function.prototype); //true
console.log(A.prototype === a.__proto__); //true

//先看a这条链
console.log(a.__proto__.__proto__ === A.prototype.__proto__); //true
console.log(a.__proto__.__proto__ === Object.prototype);//true
console.log(a.__proto__.__proto__.__proto__ === Object.prototype.__proto__);//true
console.log(a.__proto__.__proto__.__proto__ === null);//true


//再看看A这条条链
console.log(A.__proto__.__proto__ === Function.prototype.__proto__);//true
console.log(A.__proto__.__proto__ === Object.prototype);//true
console.log(A.__proto__.__proto__.__proto__ === Object.prototype.__proto__);//true
console.log(A.__proto__.__proto__.__proto__ === null);//true

prototype

参考 从 __proto__ prototype 说起

文章目录
  1. 1. Function中
    1. 1.0.1. 第一种情况 对象中有say
    2. 1.0.2. 第二种情况 原型上有say
    3. 1.0.3. 第三种情况 两个都有say,执行哪一个?
    4. 1.0.4. Person 方法的__proto__
  • 2. Object中
  • ,