- forEach
Array.prototype.forEach = function(fn, thisArg) {
var _this;
if (typeof fn !== "function") {
throw "参数必须为函数";
}
if (arguments.length > 1) {
_this = thisArg;
}
if (!Array.isArray(arr)) {
throw "只能对数组使用forEach方法";
}
for (let index = 0; index < arr.length; index++) {
fn.call(_this, arr[index], index, arr);
}
};
2.class
- set
function Set(){
var items = {}
var length = 0;
//判断元素是否存在
this.has = function(val){
return items.hasOwnProperty(val)
}
//增加操作
this.add = function(val){
if(!this.has(val)){
items[val] = val;
length++;
return true;
}
return false;
}
// 删除操作
this.remove = function(val){
if(this.has(val)){
delete items[val]
length-=1;
return true;
}
return false;
}
// 清除
this.clear = function(){
items = {};
length = 0
return true
}
//获取大小
this.size = function(){
return length;
}
//获取属性
this.values = function(){
return Object.keys(items);
}
}
var set = new Set()
set.add(1);set.add(2);set.add(3);set.add('a')
原文链接:https://blog.csdn.net/zdhui_fly/article/details/81118796
4.map
function Mymap() { //构造函数
this.init();
}
//初始化函数,创建桶(数组),每个位置都是一个对象,每个对象的属性上设置next属性,并且初始化为null。
Mymap.prototype.init = function () {
this.tong = new Array(8);
for (var i = 0; i < 8; i++) {
this.tong[i] = new Object();
this.tong[i].next = null;
}
};
//添加数据。
Mymap.prototype.set = function (key, value) {
var index = this.hash(key); //获取到当前设置的key设置到那个位置上
var TempBucket = this.tong[index]; //获取当前位置的对象
while (TempBucket.next) { //遍历如果当前对象链接的下一个不为空
if (TempBucket.next.key == key) { //如果要设置的属性已经存在,覆盖其值。
TempBucket.next.value = value;
return; //return ,不在继续遍历
} else {
TempBucket = TempBucket.next; //把指针指向下一个对象。
}
}
TempBucket.next = { //对象的next是null ,添加对象。
key: key,
value: value,
next: null
}
};
//查询数据
Mymap.prototype.get = function (key) {
var index = this.hash(key);
var TempBucket = this.tong[index];
while(TempBucket){
if(TempBucket.key == key){
return TempBucket.value;
}else{
TempBucket = TempBucket.next;
}
}
return undefined;
}
//删除数据
Mymap.prototype.delete = function(key){
var index = this.hash(key);
var TempBucket = this.tong[index];
while(TempBucket){
if(TempBucket.next.key == key){
TempBucket.next = TempBucket.next.next;
return true;
}else{
TempBucket = TempBucket.next;
}
}
}//看当前属性是否存在
Mymap.prototype.has = function(key){
var index = this.hash(key);
var TempBucket = this.tong[index];
while(TempBucket){
if(TempBucket.key == key){
return true;
}else{
TempBucket = TempBucket.next;
}
}
return false;
}//清空这个map
Mymap.prototype.clear = function(){
this.init();
}
//使设置的属性平均分配到每个位置上,使得不会某个链条过长。
Mymap.prototype.hash = function (key) {
var index = 0;
if (typeof key == "string") {
for (var i = 0; i < 3; i++) {
index = index + isNaN(key.charCodeAt(i)) ? 0 : key.charCodeAt(i);
}
}
else if (typeof key == 'object') {
index = 0;
}
else if (typeof key == 'number') {
index = isNaN(key) ? 7 : key;
} else {
index = 1;
}
return index % 8;
}
var map = new Mymap(); //使用构造函数的方式实例化
mapmap.set('name','zwq');
map.get('name');
map.has('name);
5.reduce
Array.prototype.myRedece = function(reducer, initValue){
for (let i = 0; i < this.length; i++) {
initValue = reducer(initValue, this[i], i, this)
}
return initValue
}
const res = arr.myRedece(function(val, item){
return val + item
}, 0)
原文链接:https://blog.csdn.net/m0_37068028/article/details/83866756
6.instanceof
function instance_of(L, R) {
var O = R.prototype;
L = L.__proto__;
while (true) {
if (L === null)
return false;
if (O === L)
return true;
L = L.__proto__;
}
}
7.new 以构造器的prototype属性为原型,创建新对象; 将this(也就是上一句中的新对象)和调用参数传给构造器,执行; 如果构造器没有手动返回对象,则返回第一步创建的新对象,如果有,则舍弃掉第一步创建的新对象,返回手动return的对象。
es6
function (Parent, ...rest) {
// 1.以构造器的prototype属性为原型,创建新对象;
let child = Object.create(Parent.prototype);
// 2.将this和调用参数传给构造器执行
let res=Parent.apply(child, rest);
// 3.如果构造器没有手动返回对象,则返回第一步的对象
return typeof res === 'object' ? res : child;
};
es5
function (Parent, ...rest) {
// 1.以构造器的prototype属性为原型,创建新对象;
let child={}
child._proto_=Parent.prototype
// 2.将this和调用参数传给构造器执行
let res=Parent.apply(child, rest);
// 3.如果构造器没有手动返回对象,则返回第一步的对象
return typeof res === 'object' ? res : child;
};