js 深入知识

栏目: JavaScript · 发布时间: 6年前

内容简介:<pre>function _instanceof(A, B) {}

1 手写instanceof

<pre>

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__;
}

}

</pre>

2 深拷贝

<pre>

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;
}

</pre>

3 数组降维

<pre>

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
},[])

</pre>

4 tofixed返回string

<pre>

let aa = 10937843.44;

console.log(typeof aa.toFixed(3));

</pre>

5 函数声明和函数表达式

<pre>

let test = function aa(){} // 这是一个表达式,表达式忽略名字的

let test1 = function(){}

console.log(test)

console.log(test.name) // aa

console.log(test1.name) //test1

console.log(aa)

</pre>

6 函数形参和实参

<pre>

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)

</pre>

7 js 执行顺序

  • 1 语法分析
  • 2 预编译 发生在函数执行的前一刻

函数声明整体提升,变量只是声明提升

预编译的过程(主要是读变量声明)

<pre>

// 1. 创建AO对象(Active Object)

// 2. 查找函数形参及函数内变量声明,形参名及变量名作为AO对象的属性,值为undefined

// 3. 实参形参相统一,实参值赋给形参

// 4. 查找函数声明,函数名作为AO对象的属性,值为函数引用

全局的就是 GO 就是window

function test(){

console.log(b)

if(a){

var b = 10; // 不要管if ,预编译看到声明就处理

}

}

</pre>

  • 3 解释执行

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具