function checkNull(val) {
return typeof val === 'object' && val === val;
}
checkNull()
复制代码
function checkNaN(val) {
return val.toSring() === 'NaN';
}
checkNull()
复制代码
typeof typeof() 是一个一元运算符,能够不加括号,是等价的, 返回一个类型字符串 用法==>> typeof typeof()vue
有六种结果==>: 'number', 'string', 'boolean', 'object'[null], 'function'[Symobl], 'undefined'
typeof null = > 'object'
typeof Symobl('es6') => 'function'
typeof typeof typeof typeof [] => 'string'
复制代码
instanceof 只要在原型链上便可, 用法==>> 值 instanceof 类react
[] instanceof Array // true
[] instanceof Object // true
null instanceof Object //false null空对象使用
复制代码
机制是:实例.__proto ===类的.prototype.constructr
function fn() {}
[].constrcutor === Array; / false
fn.constrcutor === Object // false
fn.constructor === Function //true
function A() {}
A.prototype.getX() {};
function B() {}
B.protype = new A();
B.prototype.constructor = B; //====>>手动设置constructor的值,设置会执行父类A
复制代码
1.封装检测数据类型和判断数据类型各个方法
const tool = function(window, undefined) {
let box = {
checkTypeof(val) {
return Object.prototype.toString.call(val);
},
isNumber(val) {
return Object.prototype.toString.call(val) === '[object Number]';
},
isString(val) {
return Object.prototype.toString.call(val) === '[object String]';
},
isBoolean(val) {
return Object.prototype.toString.call(val) === '[object Boolean]';
},
isNull(val) {
return Object.prototype.toString.call(val) === '[object Null]';
},
isUndefined(val) {
return Object.prototype.toString.call(val) === '[object Undefined]';
},
isFunction(val) {
return Object.prototype.toString.call(val) === '[object Function]';
},
isObject(val) {
return Object.prototype.toString.call(val) === '[object Object]';
},
isArray(val) {
return Object.prototype.toString.call(val) === '[object Array]';
},
isSymbol(val) {
return Object.prototype.toString.call(val) === '[object Symbol]';
},
isExpRep(val) {
return Object.prototype.toString.call(val) === '[object ExpRep]';
},
};
return box;
}();
2. 如何判断一个变量是不是数组
- instanceof val instanceof Array
- constrcutor val.constrcutor === Array
- Array.isArray(val)
- Object.prototype.toString.call(val) ==> '[object Array]' => 最靠谱,最安全
注意:constrcutor和isArray在两个iframe,在哪一个iframe中间判断,由于是俩个window对象,会有问题
复制代码
push => pop
unshift => shift
splice(n, m, val) 截取 从n,删除m个元素,用val去替换
slice(n,[m]) 筛选,从索引n,查找到索引m,不包含m
conact 合并数组
reverse 倒叙 判断回文数面试题
sort((a,b) => a-b); 排序, 原理是冒泡排序 手写冒泡排序
indexOf([n]) => lastIndexOf([n]) 原理是,遍历进行===进行比较,因此NaN是找不到的 NaN === NaN =>false 查找这个东西,返回索引值,没有返回-1,前,后查找
join 手拉手 转为字符串 split劈开转为数组
复制代码
forEach 遍历
filter 过滤,筛选出符合条件的数据
map 映射, 一组数据对应
reduce 计算
some 所有是否符合
every 任意一个符合
myForEach:
Array.prototype.myForEach = function (callback) {
if (typeof callback == 'function') {
for (let i = 0; i < this.length; i++) {
callback.call(this, this[i], i, this);
}
}
};
myMap:
Array.prototype.myMap = function (callback) {
let ary = [];
if (typeof callback == 'function') {
for (let i = 0; i < this.length; i++) {
ary.push(callback.call(this, this[i], i, this));
}
}
return ary;
};
myFilter:
Array.prototype.myFilter = function (callback) {
let ary = [];
if (typeof callback == 'function') {
for (let i = 0; i < this.length; i++) {
if (callback.call(this, this[i], i, this)) {
ary.push(this[i]);
}
}
}
return ary;
};
复制代码
find => findIndex 数组实例的find方法,用于找出第一个符合条件的数组成员
fill(val) 快速填充数组,
includes =>原理Ojbect.is(),因此他能够处理数组里面的NaN值,查找某个元素在数组里面,找到返回true,反之false
复制代码
push pop shift unshift splice sort reverse
其余返回原数组,或者布尔值,索引
复制代码
Array.prototype.mySort = function() {
for(let i=0;i<this.length-1;i++) {
for(let k=0;k=this.length-1-i;k++) {
if(this[k] > this[k+1]) {
let templ = this[k];
this[k] = this[k+1];
this[k+1] = this[k];
}
}
}
return this;
};
复制代码
let ary = new Array(1000).fill(1);
console.log(ary);
复制代码
Array.prototype.myUnique = function () {
let ary = [];
for (let i = 0; i < this.length; i++) {
console.log(ary.includes(this[i]))
if (!ary.includes(this[i])) {
ary.push(this[i]);
}
}
return ary;
}
let s= ([1, 2, 2, 1, 3, 4, 1, 2, 3,NaN, null,NaN].myUnique())
复制代码
Array.prototype.myUnique = function () {
this.sort((a, b) => a-b);
for(let i=0;i<this.length;i++) {
if (this[i] === this[i+1]) { ==>>Object.is(this[i], this[i+1])
this.splice(i ,1);
i--;
continue;
}
}
return this;
}
var s= [1, 2, 2, 1, 3, 4, 1, 2, 3,NaN, null,NaN].myUnique();
[null, 1, 2, 3, 4, NaN, NaN]; 去不掉NaN, 由于NaN === NaN;是不相等的,NaN除了数字其余东西,鬼知道呢
====>> 优化去掉NaN;
this[i] === this[i+1]不能够排除,咱们用Object.is(this[i], this[i+1])不就能够了嘛
复制代码
Array.prototype.myUnique = function () {
let obj = {};
for (let i = 0; i < this.length; i++) {
let cur = this[i];
if (obj[cur] === cur) { // ===>> Object.is(obj[cur] === cur)
this.splice(i, 1);
i--;
continue;
}
obj[cur] = cur;
}
obj = null; // ===>>释放内存,优化
return this; // ===>> 链式调用
}
var s = [1, 2, 2, 1, 3, 4, 1, 2, 3, NaN, null, NaN].myUnique();
s => [1, 2, 3, 4, NaN, null, NaN]
也是严格对NaN问题,Object.is(obj[cur] === cur)
复制代码
Array.prototype.myUnique = function () {
return [... new Set(this)]
}
var s = [1, 2, 2, 1, 3, 4, 1, 2, 3, NaN, null, NaN].myUnique();
s ==> [1, 2, 3, 4, NaN, null]
复制代码
function toArray(likeAry) {
if (likeAry && likeAry.length) {
let ary = [];
for (let i = 0; i < this.length; i++) {
ary.push(this[i]);
}
return ary;
}
return [];
}
console.log(toArray(document.getElementsByTagName('*')) instanceof Array) // true
复制代码
function fn() {
arguments = Array.prototype.slice(arguments);
arguments = Array.from(arguments);
}
复制代码
function toArray(likeAry) {
return Array.from(likeAry);
}
复制代码
Array.prototype.myForEach = function(callback) {
if (typeof callback == 'function') {
for (let i =0;i<this.length;i++) {
callback.call(this,this[i], i, this);
}
}
};
var ary = ['vue', 'react', 'angular'];
ary.myForEach(function(item, index, slef) {
console.log(item, index, slef)
});
复制代码
1.for循环 // ==>> 不会遍历出私有的属性
let ary = [1, 2, 3];
ary.b = 100;
for (let i=0,len=ary.length;i<len;i++) {
console.log(ary);
}
2. forEach // ==>> 不能return,不会遍历出私有的属性
ary.forEach((item, index, self)=> {
console.log(item);
});
3.for - in // ==>> key会变成字符串,而且会遍历出私有的属性,利用hasOwnproperty进行过滤
for(let key in ary) {
if (!ary.hasOwnproperty(key)) {
conosle.log(key)
}
}
4.for - of // ==>> 能够return, 可是必须是数组,不能遍历对象
for(let key of ary) {
}
实现想遍历对象: Object.keys(); //====>>将一个对象的属性依次放到一个数组中,返回一个新的数组
let obj = {school:'科师',age:9};
for (let key of Ojbect.keys(obj) {
console.log(obj[key]);
}
复制代码
chartAt(index) 返回子字符串,index为字符串下标,index取值范围[0,str.length-1]
chatCodeAt(index) 返回子字符串的unicode编码,index取值范围同上
查找方法:
indexOf(searchString,startIndex)返回子字符串第一次出现的位置,从startIndex开始查找,找不到时返回-1
lastIndexOf(searchString,startIndex) 从由往左找子字符串,找不到时返回-1
截取方法
substr(start,end) 两个参数都为正数,返回值:[start,end) 也就是说返回从start到end-1的字符
substring(start,end) 返回从 start 到 end(不包括)之间的字符,start、end均为 非负整数。若结束参数(end)省略,则表示从start位置一直截取到最后。
slice 两个参数可正可负,负值表明从右截取,返回值:[start,end)也就是说返回从start到end-1的字符
字符串分割成数组
str.split(separator,limit); 参数1指定字符串或正则,参照2指定数组的最大长度
- str.split(""); 每一个字符都被分割 ['','','','']
- str.split(); 整个字符串放到数组里 ['']
str.replace(rgExp/substr,replaceText) 返回替换后的字符串
英文转换
toLowerCase() 转小写
toUpperCase() 转大写
复制代码
后续天天更新es6