JavaScript中的原型链
2019-07-12 15:06:44 1 举报
JavaScript原型链完整结构
作者其他创作
大纲/内容
+ constructor
+ prototype
一、函数有3种形式,2种用途# 形式1,函数声明,用途1:函数调用,比如foo1()function foo1(){}# 形式2,函数表达式,使用箭头函数,用途1:函数调用,比如foo2()var foo2 = ()=>{}# 形式3,函数表达式,使用正常函数,用途1:函数调用,比如foo3()var foo3 = function(){}# 用途2:当构造函数,比如前面的var student3 = new Student(\"小红\
由【xx.protype】指向【xx.protype对象】的“原型对象链”
Student.prototype.__proto__ === Object.prototype
Object.prototype.constructor === Object即student1-3constructor === Object
typeof: 'function'Function
typeof: 'object'foo3 {}即是foo3.prototype
+ __proto__
Array.prototype.__proto__ === Object.prototype
+ other attrs
typeof: 'object'arr1
arr1.__proto__ === Array.prototype
foo1.prototype.__proto__ === Object.prototype
typeof: 'function'Student
foo1.constructor
Student.prototype === Student {}
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数instance.constructor实际上是instance.__proto__.constructor,所以student1-2.constructor是Object,而student3.constructor是Student;
typeof: 'object'arr2
typeof: '实例类型'实例
undefined
typeof: 'function'foo2
foo3.prototype.__proto__ === Object.prototype
typeof: 'function'Function.prototype
+ consturctor
typeof: 'object'Student {}即是Student.prototype
typeof: 'function'Array
student2.__proto__ === Object.prototype
foo3.__proto__ === Function.prototype
Object.__proto__ === Function.prototype
arr3.__proto__ === Array.prototype
Function.prototype
Function.prototype.__proto__ === Object.prototype,形成了一个闭环
Function.prototype.constructor === Function说明Function创建了Function自己
Function.__proto__ === Function.prototype
Array.prototype.constructor
Student.__proto__ === Function.prototype
- 对象Object
+ other attrs - name: '小明' - age: 16
typeof: 'object'student1
由xx.prototype.constructor指向构造函数的构造链
一、对象有3种创建方式# 1. 使用{}直接定义对象var student1 = { name: \"小明\
student3.__proto__ === Student.prototype
foo3.constructor
typeof: '基础对象/类型/函数的类型'基础对象/类型/函数,比如Object/Function/Array..
图例
typeof: 'object'[]即是Array.prototype
Object.prototype.__proto__ === null
Object.prototype === {}
typeof: 'function'Object
+ other attrs - name: '小红' - age: 18 - ShowInfo: [Function]
由instance.__proto__指向Base.prototype的“原型链”
+ other attrs - name: '小军' - age: 17
student1.__proto__ === Object.prototype
typeof: 'object'student2
null
foo1.__proto__ === Function.prototype
typeof: 'function'foo3
foo2.__proto__ === Function.prototype
typeof: 'function'foo1
- 构造函数Constructor
Array.__proto__ === Function.prototype
arr2.__proto__ === Array.prototype
- 函数Function
typeof: 'object'arr3
Stuent.prototype.constructor === Student本身
typeof: '原型对象类型'原型对象
typeof: 'object'foo1 {}即是foo1.prototype
收藏
0 条评论
下一页