zoukankan      html  css  js  c++  java
  • JavaScript易混淆的零碎知识点积累

    1、callee属性 和 caller属性。

    区别:两者的调用对象不同

    arguments.callee:指向拥有这个arguments对象的函数,在递归运算中经常用到。

    functionName.caller:在函数中通过函数名来调用,这个属性中保存着调用当前函数的函数引用。functionName也可以使用arguments.callee来代替 。 

    function out(){
        console.log(arguments.callee);
        }    
    out();  // ƒ out(){console.log(arguments.callee);} 
    function out(){
        inner();
    }
    function inner(){
        console.log(arguments.callee.caller)
    }
    out(); // ƒ out(){inner();}

    2、arguments.length 和 function.length

    区别:很明显,调用这个属性的对象不同。arguments.length表示调用函数时实际传递的参数个数,有可能与函数定义时设置的参数个数不同。function.length表示函数希望接收的参数个数即定义函数时声明的形参的个数。

    function test(a,b){
        console.log('arguments.length = '+arguments.length);
        console.log('函数test.length = '+test.length);
    }
    test(1,2,3,4); // arguments.length = 4, 函数test.length = 2

    3、在ECMAscript5中,prototype属性时不可枚举的,因此使用for-in无法获取该属性的值。

    4、apply方法和call方法

    这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体this对象的值。

    区别:主要是除了第一个参数之外的其他参数类型不同。apply接收arguments对象和数组,call只能逐个传入参数。

    function sum(num1,num2){
            return num1 + num2;
    }
    function callSum1(num1,num2){
            return sum.apply(this,arguments); // 传入arguments对象
    }
    function callSum2(num1,num2){
            return sum.apply(this,[num1,num2]); // 传入数组
    }
    function callSum3(num1,num2){
            return sum.call(this,num1,num2); //逐个列举参数
    }
    console.log(callSum1(10,10)); //20
    console.log(callSum2(10,10)); //20
    console.log(callSum3(10,10)); //20

     5、typeof 和 instanceof

    这两个操作符都可以用来检测类型,但是typeof只能检测基本数据类型,比如:字符串、数值、布尔值、undefined等,如果变量是一个对象或者null,使用typeof只会返回“object”。

    使用instanceof可以知道检测的值是什么类型的对象。

    var num = 1;
    var str = 'hello';
    var b = true;
    var u;
    var n = null;
    var arr = [1,2,3];
    
    console.log(typeof num); //number
    console.log(typeof str); // string
    console.log(typeof b); // boolean
    console.log(typeof u); // undefined
    console.log(typeof n); // object
    console.log(typeof arr); // object
    console.log(arr instanceof Array); // true

     6、document.documentElement.scrollTop 和 document.body.scrollTop

    页面具有 DTD(或者说指定了 DOCTYPE)时,使用 document.documentElement。
    页面不具有 DTD(或者说没有指定了 DOCTYPE)时,使用 document.body。
    为了兼容,可以使用如下代码:

    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop 

    不断学习,不断完善中~~~

  • 相关阅读:
    git 命令速查及使用
    Centos6.5 LAMP环境源码包安装与配置,附安装包百度网盘地址 (转做笔记)
    不再为Apache进程淤积、耗尽内存而困扰((转))
    centos6.5 安装linux 环境
    window 配置wnmp(转下整理 ,全)
    mac下安装 xampp 无法启动apache (转,留用)
    Git命令行(转用于学习和记录)
    apache 局域网访问
    华为云GaussDB(for opengauss)如何绑定公网,实现putty的远程访问gaussdb数据库。
    Day9 打卡acwing.429 奖学金
  • 原文地址:https://www.cnblogs.com/happypayne/p/7747491.html
Copyright © 2011-2022 走看看