• Function


    一、Function.call()函数

        1、a.call(b)​​;    //包含第三点[将.call(b)前面的对象传递给b对象] //很像继承

            将a对象(所有)传递给b函数对象​​​​;

        2、a.aMethod.call(b)​;    //不会覆盖b中的同名方法

            将​a的aMethod方法传递给b函数对象;

        3、测试代码如下:

    1. function A(nameA){
    2.   this.name=nameA;
    3.   this.showName=function(){
    4.     return this.name;
    5.   }
    6. }
    7. function B(nameB){
    8.   A.call(this,nameB);
    9.   this.showTest=function(){
    10.     return nameB;
    11.   }
    12. //重名方法
    13.   //this.showName=function(){
    14.    // return "Soul";
    15. //  }
    16. }
    17. var b=new B("B");
    18. console.log(b.name+" "+b.showName()+" "+b.showTest());  //B B B(去掉注释则结果为:B Soul B)
     
     
    二、Function.apply函数
        
        1、Function.apply(obj,arguments)
     
            args参数将会依次(从arguments[0]开始)传递给Function函数
     
        2、代码示例如下:
     
    1. function A(nameA){
    2.   this.name=nameA;
    3.   this.showName=function(){
    4.     return this.name;
    5.   }
    6. }
    7. function B(nameA,nameB){//参数将依次传给A(),因此A的参数要写在前面
    8.   //var arr=new Array();
    9.   //arr[0]=nameB;
    10.   //A.apply(this,arr);    //参数一定为数组
    11.   A.apply(this,arguments);
    12.   this.showTest=function(){
    13.     return nameB;
    14.   }
    15. }
    16. var b=new B("A","B");
    17. console.log(b.name+" "+b.showName()+" "+b.showTest());    //A A B
      3、apply其他用法
     
        (1)、Math.max.apply(null,arr);     //返回数组arr中的最大值
     
         (2)、Array.prototype.push.apply(arr1,arr2);    //将arr2添加到arr1中   arr1本身改变
     
    三、Function.bind()函数
     
      1、该方法可以创建一个新函数,该函数定义了函数内的this关键字,并可以指定初始化参数值
     
      2、语法格式:oFunction.bind(thisObjet[,parameters]);
     
      3、将this绑定到thisObject上
     
    All rights reserved please indicate the source if reprint---吓尿了的大肥鼠
  • 相关阅读:
    linux的别名(alias/unalias)
    asp.net <%%> <%#%><%=%><%@%><%$%>用法与区别
    SQL获取刚插入的记录的自动增长列ID的值
    包和继承
    面对对象编程(封装)
    面对对象编程(上)
    数组(下)
    java数组-如何在一堆数据中使用数组!
    Request和Response学习笔记5
    Request和Response学习笔记4
  • 原文地址:https://www.cnblogs.com/realsoul/p/5511600.html
Copyright © 2020-2023  润新知