zoukankan      html  css  js  c++  java
  • JS手写call、bind、apply

    call方法的实现

      Function.prototype.MyCall = function(content,...args){

        const self = content || window;

        const args = args.slice(1)

        const self.fn = this

        const result = self.fn(args)

        delete self.fn

        return result

      }

    apply方法实现,和call方法差不多

      Function.prototype.MyApply = function(content,args = []){

        const self = content || window

        self.fn = this;

        let result = null

        if(args.length > 0){

          const  = self.fn(args)

        }else {

          result = self.fn()

        }

        delete self.fn

        return result

      }

    稍显麻烦的bind方法

     Function.prototype.MyBind = function(content){

       const self = this

       const args = arguments.slice(1)

       return function F(){

         if(this instanceof F){   //通过new的方式

           return new self(...args,...arguments)

         }else{

           const doubleArgs = args.concat(arguments)

           return self.apply(content,doubleArgs)

            }

       }

     }

  • 相关阅读:
    ListView
    ScrollView-电影列表
    ScrollView
    Image组件
    TextInput
    Touchable类组件
    Text
    View
    FlexBox
    StyleSheet
  • 原文地址:https://www.cnblogs.com/vnwith/p/12606160.html
Copyright © 2011-2022 走看看