最详尽的 JS 原型与原型链终极详解(三)

Array.proto === Function.prototype // true

Array.constructor == Function //true

RegExp.proto === Function.prototype // true

RegExp.constructor == Function //true

Error.proto === Function.prototype // true

Error.constructor == Function //true

Date.proto === Function.prototype // true

Date.constructor == Function //true

JavaScript中有内置(build-in)构造器/对象共计12个(ES5中新加了JSON),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎创建,Math,JSON是以对象形式存在的,无需new。它们的proto是Object.prototype。如下

Math.proto === Object.prototype // true

Math.construrctor == Object // true

JSON.proto === Object.prototype // true

JSON.construrctor == Object //true

上面说的函数对象当然包括自定义的。如下

// 函数声明

function Person() {}

// 函数表达式

var Perosn = function() {}

console.log(Person.proto === Function.prototype) // true

console.log(Man.proto === Function.prototype) // true

这说明什么呢?

** 所有的构造器都来自于 Function.prototype,甚至包括根构造器ObjectFunction自身。所有构造器都继承了·Function.prototype·的属性及方法。如length、call、apply、bind**

(你应该明白第一句话,第二句话我们下一节继续说,先挖个坑:))

Function.prototype也是唯一一个typeof XXX.prototype为 functionprototype。其它的构造器的prototype都是一个对象(原因第三节里已经解释过了)。如下(又复习了一遍):

console.log(typeof Function.prototype) // function

console.log(typeof Object.prototype) // object

console.log(typeof Number.prototype) // object

console.log(typeof Boolean.prototype) // object

console.log(typeof String.prototype) // object

console.log(typeof Array.prototype) // object

console.log(typeof RegExp.prototype) // object

console.log(typeof Error.prototype) // object

console.log(typeof Date.prototype) // object

console.log(typeof Object.prototype) // object

噢,上面还提到它是一个空的函数,console.log(Function.prototype) 下看看(留意,下一节会再说一下这个)

知道了所有构造器(含内置及自定义)的__proto__都是Function.prototype,那Function.prototype__proto__是谁呢?

相信都听说过JavaScript中函数也是一等公民,那从哪能体现呢?如下

console.log(Function.prototype.__proto__ === Object.prototype) // true

这说明所有的构造器也都是一个普通 JS 对象,可以给构造器添加/删除属性等。同时它也继承了Object.prototype上的所有方法:toString、valueOf、hasOwnProperty等。(你也应该明白第一句话,第二句话我们下一节继续说,不用挖坑了,还是刚才那个坑;))

最后Object.prototype的proto是谁?

Object.prototype.__proto__ === null // true

已经到顶了,为null。(读到现在,再回过头看第五章,能明白吗?)

八. Prototype

在 ECMAScript 核心所定义的全部属性中,最耐人寻味的就要数 prototype 属性了。对于 ECMAScript 中的引用类型而言,prototype 是保存着它们所有实例方法的真正所在。换句话所说,诸如 toString()和 valuseOf() 等方法实际上都保存在 prototype 名下,只不过是通过各自对象的实例访问罢了。

——《JavaScript 高级程序设计》第三版 P116

我们知道 JS 内置了一些方法供我们使用,比如:

对象可以用 constructor/toString()/valueOf() 等方法;

数组可以用 map()/filter()/reducer() 等方法;

数字可用用 parseInt()/parseFloat()等方法;

Why ???

why??

当我们创建一个函数时:

var Person = new Object()

Person 是 Object 的实例,所以 Person 继承Object 的原型对象Object.prototype上所有的方法:

Object.prototype

Object 的每个实例都具有以上的属性和方法。

所以我可以用 Person.constructor 也可以用 Person.hasOwnProperty

当我们创建一个数组时:

var num = new Array()

num 是 Array 的实例,所以 num 继承Array 的原型对象Array.prototype上所有的方法:

Array.prototype

Are you f***ing kidding me? 这尼玛怎么是一个空数组???

doge

我们可以用一个 ES5 提供的新方法:Object.getOwnPropertyNames

获取所有(包括不可枚举的属性)的属性名不包括 prototy 中的属性,返回一个数组:

var arrayAllKeys = Array.prototype; // [] 空数组

// 只得到 arrayAllKeys 这个对象里所有的属性名(不会去找 arrayAllKeys.prototype 中的属性)

console.log(Object.getOwnPropertyNames(arrayAllKeys));

/* 输出:

[“length”, “constructor”, “toString”, “toLocaleString”, “join”, “pop”, “push”,

“concat”, “reverse”, “shift”, “unshift”, “slice”, “splice”, “sort”, “filter”, “forEach”,

“some”, “every”, “map”, “indexOf”, “lastIndexOf”, “reduce”, “reduceRight”,

“entries”, “keys”, “copyWithin”, “find”, “findIndex”, “fill”]

*/

这样你就明白了随便声明一个数组,它为啥能用那么多方法了。

细心的你肯定发现了Object.getOwnPropertyNames(arrayAllKeys) 输出的数组里并没有 constructor/hasOwnPrototype对象的方法(你肯定没发现)。

但是随便定义的数组也能用这些方法

var num = [1];

console.log(num.hasOwnPrototype()) // false (输出布尔值而不是报错)

Why ???

why??

因为Array.prototype 虽然没这些方法,但是它有原型对象(__proto__):

// 上面我们说了 Object.prototype 就是一个普通对象。

Array.prototype.proto == Object.prototype

所以 Array.prototype 继承了对象的所有方法,当你用num.hasOwnPrototype()时,JS 会先查一下它的构造函数 (Array) 的原型对象 Array.prototype 有没有有hasOwnPrototype()方法,没查到的话继续查一下 Array.prototype 的原型对象 Array.prototype.__proto__有没有这个方法。

当我们创建一个函数时:

var f = new Function(“x”,“return x*x;”);

//当然你也可以这么创建 f = function(x){ return x*x }

console.log(f.arguments) // arguments 方法从哪里来的?

console.log(f.call(window)) // call 方法从哪里来的?

console.log(Function.prototype) // function() {} (一个空的函数)

console.log(Object.getOwnPropertyNames(Function.prototype));

/* 输出

[“length”, “name”, “arguments”, “caller”, “constructor”, “bind”, “toString”, “call”, “apply”]

*/

我们再复习第八小节这句话:

所有函数对象****proto都指向 Function.prototype,它是一个空函数(Empty function)

嗯,我们验证了它就是空函数。不过不要忽略前半句。我们枚举出了它的所有的方法,所以所有的函数对象都能用,比如:

函数对象

如果你还没搞懂啥是函数对象?

去屎 | center

还有,我建议你可以再复习下为什么:

Function.prototype 是唯一一个typeof XXX.prototype为 “function”的prototype

我猜你肯定忘了。

九. 复习一下

第八小节我们总结了:

所有函数对象的 proto 都指向 Function.prototype,它是一个空函数(Empty function)

但是你可别忘了在第三小节我们总结的:

所有对象的 proto 都指向其构造器的 prototype

咦,我找了半天怎么没找到这句话……

doge | center

我们下面再复习下这句话。

先看看 JS 内置构造器:

var obj = {name: ‘jack’}

var arr = [1,2,3]

var reg = /hello/g

var date = new Date

var err = new Error(‘exception’)

console.log(obj.proto === Object.prototype) // true

console.log(arr.proto === Array.prototype) // true

console.log(reg.proto === RegExp.prototype) // true

console.log(date.proto === Date.prototype) // true

console.log(err.proto === Error.prototype) // true

再看看自定义的构造器,这里定义了一个 Person

function Person(name) {

this.name = name;

}

var p = new Person(‘jack’)

console.log(p.proto === Person.prototype) // true

p 是 Person 的实例对象,p 的内部原型总是指向其构造器 Person 的原型对象 prototype

每个对象都有一个 constructor 属性,可以获取它的构造器,因此以下打印结果也是恒等的:

function Person(name) {

this.name = name

}

var p = new Person(‘jack’)

console.log(p.proto === p.constructor.prototype) // true

上面的Person没有给其原型添加属性或方法,这里给其原型添加一个getName方法:

function Person(name) {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
droid工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-46giMrjh-1715257641117)]

[外链图片转存中…(img-O55UByhg-1715257641117)]

[外链图片转存中…(img-fMLohVBD-1715257641118)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值