JavaScript 原型以及原型链。


1. 属性介绍

  • __proto__ : 每个对象的私有属性,null以及undefined没有,在IE中只有IE11版本才有,IE10及以下没有,__proto__指向它的构造函数的原型对象(prototype).
var arr = [];
var obj = {};
var set = new Set();
var map = new Map();
var sym = Symbol();
var fn = function () {};
var date = new Date();
var reg = //d+/;
var err = new Error();
var int = new Int8Array();
var float = new Float32Array();
var bool = true;

console.log(arr.__proto__, `arr.__proto__`);
console.log(arr.__proto__ === Array.prototype, `arr.__proto__ === Array.prototype`);
console.log(obj.__proto__, `obj.__proto__`);
console.log(obj.__proto__ === Object.prototype, `obj.__proto__ === Object.prototype`);
console.log(set.__proto__, `set.__proto__`);
console.log(set.__proto__ === Set.prototype, `set.__proto__ === Set.prototype`);
console.log(map.__proto__, `map.__proto__`);
console.log(map.__proto__ === Map.prototype, `map.__proto__ === Map.prototype`);
console.log(sym.__proto__, `sym.__proto__`);
console.log(sym.__proto__ === Symbol.prototype, `sym.__proto__ === Symbol.prototype`);
console.log(fn.__proto__, `fn.__proto__`);
console.log(fn.__proto__ === Function.prototype, `fn.__proto__ === Function.prototype`);
console.log(date.__proto__, `date.__proto__`);
console.log(date.__proto__ === Date.prototype, `date.__proto__ === Date.prototype`);
console.log(reg.__proto__, `reg.__proto__`);
console.log(reg.__proto__ === RegExp.prototype, `reg.__proto__ === RegExp.prototype`);
console.log(err.__proto__, `err.__proto__`);
console.log(err.__proto__ === Error.prototype, `err.__proto__ === Error.prototype`);
console.log(int.__proto__, `int.__proto__`);
console.log(int.__proto__ === Int8Array.prototype, `int.__proto__ === Int8Array.prototype`);
console.log(float.__proto__, `float.__proto__`);
console.log(float.__proto__ === Float32Array.prototype, `float.__proto__ === Float32Array.prototype`);
console.log(bool.__proto__, `bool.__proto__`);
console.log(bool.__proto__ === Boolean.prototype, `bool.__proto__ === Boolean.prototype`);

image

  • constructor : 指向的是自己的创建者,比如,num.constructor === Number,如下图
var num = 1;
var str = '1';
var arr = [];
var obj = {};
var set = new Set();
var map = new Map();
var sym = Symbol();
var fn = function () {};
var date = new Date();
var reg = //d+/;
var err = new Error();
var int = new Int8Array();
var float = new Float32Array();
var bool = true;

console.log(num.constructor, `num.constructor`);
console.log(str.constructor, `str.constructor`);
console.log(arr.constructor, `arr.constructor`);
console.log(obj.constructor, `obj.constructor`);
console.log(set.constructor, `set.constructor`);
console.log(map.constructor, `map.constructor`);
console.log(sym.constructor, `sym.constructor`);
console.log(fn.constructor, `fn.constructor`);
console.log(date.constructor, `date.constructor`);
console.log(reg.constructor, `reg.constructor`);
console.log(err.constructor, `err.constructor`);
console.log(int.constructor, `int.constructor`);
console.log(float.constructor, `float.constructor`);
console.log(bool.constructor, `bool.constructor`);

image

  • prototype : 函数的独有属性,指向另一个对象
var arr = [];
var obj = {};
var set = new Set();
var map = new Map();
var sym = Symbol();
var fn = function () {};
var date = new Date();
var reg = //d+/;
var err = new Error();
var int = new Int8Array();
var float = new Float32Array();
var bool = true;

console.log(arr.prototype, `arr.prototype`);
console.log(obj.prototype, `obj.prototype`);
console.log(set.prototype, `set.prototype`);
console.log(map.prototype, `map.prototype`);
console.log(sym.prototype, `sym.prototype`);
console.log(fn.prototype, `fn.prototype`);
console.log(date.prototype, `date.prototype`);
console.log(reg.prototype, `reg.prototype`);
console.log(err.prototype, `err.prototype`);
console.log(int.prototype, `int.prototype`);
console.log(float.prototype, `float.prototype`);
console.log(bool.prototype, `bool.prototype`);

image

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/webdev/272086.html

(0)
上一篇 2022年7月8日 21:09
下一篇 2022年7月8日 21:11

相关推荐

发表回复

登录后才能评论