原型方式创建数组

本文介绍了如何在JavaScript中使用原型方式扩展数组的shift和unshift方法,以及原型查找机制的工作原理。同时,还展示了如何通过原型构造call()方法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用原型方式创建数组的shift方法和unshift方法

 Array.prototype.shifts = function(){
		 	for(var i=0;i<this.length-1;i++){
		 		this[i] = this[i+1];
		 	}
		 	this.length = this.length-1;
		 }
		 var arr = [11,22,33];
		 arr.shifts();
		 console.log(arr);
Array.prototype.unshifts = function(user){
		 	this.length = this.length+1;
		 	for(var i=this.length-1;i>=0;i--){
		 		this[i] = this[i-1];
		 	}
		 	this[0] = user;
		 }
		 var arr = [11,22,33];
		 arr.unshifts(44);
		 console.log(arr);

2.原型查找机制

 1. 当访问一个对象的属性或方法时,首先查找这个对象自身有没有

 2. 如果没有就查找它的原型(也就是 __proto__ 指向的prototype 原型对象 )

 3. 如果还没有找到就查找原型对象的原型(Object的原型对象)

 4. 依次类推一直找到Object为止( null )

 5. __proto__ 对象原型的意义就在于为对象成员查找机制提供一个方向,或者说一条线路

3.原型的方式构造一个call()方法

Function.prototype.callNew = function(content,...sum){

console.log(!content);

if(!content|| context === null|| context === undefined){

context = window;

}

let fn = Symbol();

content[fn] = this;

return content[fn](...sum);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值