本文主要是介绍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`);
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`);
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`);
这篇关于JavaScript 原型以及原型链。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!