封装type及数组去重作业

面试题:

var obj = {
	"2":'a',
	"3":'b',
	"length":2,
	"push":Array.prototype.push
}
obj.push('c');
obj.push('d');
//{2: "c", 3: "d", length: 4, push: ƒ}
// Array.prototype.push = function(target){
	// this[this.length] = target;
	// this.length++;
// }

封装type

//封装type,实现以下返回值
//typeof([])--》array
//typeof({})--》object
//typeof(new Number())--》number - object
//typeof(new Boolean())--》boolean - object
//typeof(new String())--》string - object
function type(target){
	var template = {
		'[object Array]':'array',
		'[object Object]':'object',
		'[object Number]':'number - object',
		'[object Boolean]':'boolean - object',
		'[object String]':'string - object'
	}
	// 1.分两类:引用值 原始值
	// 2.区分引用值
	if(target === null){//处理特殊值
		return 'null';
	}else if(typeof(target) == 'object'){
		//区分数组&对象&包装类
		var str = Object.prototype.toString.call(target);
		return template[str];
	}else{
		typeof(target);
	}
}

数组去重

Array.prototype.unique = function() {
	var obj = {
		'push':Array.prototype.push,
		'splice':Array.prototype.splice
	}
	var arr = [];
	var len = this.length;
	for(var i = 0; i < len; i++){
		if(!obj[this[i]]){
			obj[this[i]] = 'a';
			arr.push(this[i]);
		}
	}
	return arr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值