js 深入知识

1 手写instanceof
function _instanceof(A, B) {
    var O = B.prototype;// 取B的显示原型
    A = A.__proto__;// 取A的隐式原型
    while (true) {
        //Object.prototype.__proto__ === null
        if (A === null)
            return false;
        if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true
            return true;
        A = A.__proto__;
    }
}
2 深拷贝
   function deepClone(data) {
        if(typeof data === "object" && data !== null){
            var type = data.constructor;
            var result = new type();
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    result[key] = deepClone(data[key]);
                }
            }
            return result;
        }
        return data;
    }
3 数组降维
var arr = [1, 2, [3]];
var res = Array.prototype.concat.apply([], arr);
console.log(res);
var arr2 = [1];
console.log(111);
console.log(arr2.concat(11));

// es6 
let flatten = arr => arr.reduce((begin,current)=>{
        Array.isArray(current)?
        begin.push(...flatten(current)):
        begin.push(current);
        return begin
    },[])
4 tofixed返回string
let aa = 10937843.44;
console.log(typeof aa.toFixed(3));
5 函数声明和函数表达式
let test = function aa(){} //  这是一个表达式,表达式忽略名字的 
let test1 = function(){}
console.log(test) 
console.log(test.name) // aa 
console.log(test1.name) //test1 
console.log(aa)
6 函数形参和实参
function tmp(a,b){
  console.log(tmp.length) // 2 表示函数形参的个数 
}
tmp(1)
function sum(a,b,c){
  a = 11;
  console.log(arguments[0]) // 形参和实参映射关系(两个都存在才映射)
  c = 2;
  console.log(arguments[2]) // undefined  
}
sum(1,2)
7 js 执行顺序
  • 1 语法分析
  • 2 预编译 发生在函数执行的前一刻

函数声明整体提升,变量只是声明提升
预编译的过程(主要是读变量声明)

// 1. 创建AO对象(Active Object) 
// 2. 查找函数形参及函数内变量声明,形参名及变量名作为AO对象的属性,值为undefined 
// 3. 实参形参相统一,实参值赋给形参 
// 4. 查找函数声明,函数名作为AO对象的属性,值为函数引用
全局的就是GO 就是window 
function test(){
  console.log(b)
  if(a){
    var b = 10; // 不要管if ,预编译看到声明就处理 
   }
}
  • 3 解释执行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值