Function与Object
发布日期:2021-05-09 00:28:28 浏览次数:14 分类:博客文章

本文共 2249 字,大约阅读时间需要 7 分钟。

Function与Object

JavaScript中内置了两个顶级对象FunctionObjectObject是所有对象的基类,而所有的构造函数同时又是Function对象的实例。

Object

JavaScript中的所有对象都来自Object,所有对象从Object.prototype继承方法和属性,尽管它们可能被覆盖,例如其他构造函数在原型中实现自己的toString()方法。Object原型对象的更改将传播到所有对象,除非这些受到更改的属性和方法沿原型链被覆盖。

// 定义三个对象var a = function(){} // 构造函数对象var b = new Array(1); // 数组对象var c = new Number(1); // 数字对象 // 包装对象// 检查原型链console.log(a.__proto__.__proto__ === Object.prototype); // trueconsole.log(b.__proto__.__proto__ === Object.prototype); // trueconsole.log(c.__proto__.__proto__ === Object.prototype); // true// 拆分指向console.log(a.__proto__ === Function.prototype); // trueconsole.log(Function.prototype.__proto__ === Object.prototype); // trueconsole.log(b.__proto__ === Array.prototype); // trueconsole.log(Array.prototype.__proto__ === Object.prototype); // trueconsole.log(c.__proto__ === Number.prototype); // trueconsole.log(Number.prototype.__proto__ === Object.prototype); // true// 使用instanceof 实际也是检测原型链// instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上console.log(a instanceof Object); // trueconsole.log(b instanceof Object); // trueconsole.log(c instanceof Object); // true

Function

JavaScript中的所有的构造函数都继承自Function,包括Object构造函数,Function构造函数也继承于自己,当然Function也是继承于Object.prototype,可以说是先有的Object.prototypeObject.prototype构造出Function.prototype,然后Function.prototype构造出ObjectFunction

// 构造函数对象var a = function(){} // 构造函数对象// 检查原型链console.log(a.__proto__ === Function.prototype); // trueconsole.log(Object.__proto__ === Function.prototype); // trueconsole.log(Function.__proto__ === Function.prototype); // trueconsole.log(Function.prototype.__proto__ === Object.prototype); // true// 使用instanceof console.log(a instanceof Function); // trueconsole.log(Object instanceof Function); // trueconsole.log(Function instanceof Function); // true

总结

  • 一切对象都继承于Object,都是从Object.prototype继承方法和属性。
  • 一切构造函数包括ObjectFunction,都继承于Function,最终继承于Object

每日一题

https://github.com/WindrunnerMax/EveryDay

参考

https://www.cnblogs.com/tiancai/p/7463252.htmlhttps://www.cnblogs.com/yf2196717/p/10989466.htmlhttps://www.cnblogs.com/ioveNature/p/6880176.htmlhttps://www.cnblogs.com/tiffanybear/p/11320651.htmlhttps://blog.csdn.net/backee/article/details/83378772https://blog.csdn.net/weixin_34237596/article/details/88026745
上一篇:浏览器页面呈现过程
下一篇:前端性能优化方案

发表评论

最新留言

不错!
[***.144.177.141]2025年05月04日 16时05分04秒