zoukankan      html  css  js  c++  java
  • 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---吓尿了的大肥鼠
  • 相关阅读:
    返回一个随机数组中的子数组中的数相加最大的和
    四则运算二之结果
    四则运算二
    UVA 11741 Ignore the Blocks
    UVA 1408 Flight Control
    UVA 10572 Black & White
    CF1138D(545,div2) Camp Schedule
    UVA 1214 Manhattan Wiring
    UVA 11270 Tiling Dominoes
    BZOJ 3261 最大异或和
  • 原文地址:https://www.cnblogs.com/realsoul/p/5511600.html
Copyright © 2011-2022 走看看