네 이 티 브 ES5 패키지 콜,apply,bid 등 을 사용 합 니 다.

1957 단어 javascriptes5
코드 를 직접 올 리 면 모두 가 한눈 에 알 수 있다.
html>


  
  
  
  Document


  

call-apply-bind

       'use strict'     Function.prototype.call = function () { //  es5       var targetFuc = this;       var thisArg = arguments[ 0 ]       if ( !(typeof thisArg === 'object' || typeof thisArg === 'function') ) {         throw new Error( 'type error' )       }       thisArg.nestFuc = targetFuc       var evalString = 'thisArg.nestFuc('       for ( var i = 1; i < arguments.length; i++ ) {         evalString += 'arguments[' + i + '],'       }       evalString += ')'       console.log( evalString )       return eval( evalString )     }     Function.prototype.apply = function ( thisArg, argArray ) {       if ( !(typeof thisArg === 'object' || typeof thisArg === 'function') ) {         throw new Error( 'type error' )       }       var targetFuc = this;       thisArg.nestFuc = targetFuc       var evalString = 'thisArg.nestFuc('       for ( var i = 0; i < argArray.length; i++ ) {         evalString += 'argArray[' + i + '],'       }       evalString += ')'       console.log( evalString )       return eval( evalString )     }     Function.prototype.bind = function () {       var targetFuc = this;       var slice = Array.prototype.slice       var argumentsArray = slice.call( arguments )       var thisArg = argumentsArray[ 0 ]       if ( !(typeof thisArg === 'object' || typeof thisArg === 'function') ) {         throw new Error( 'type error' )       }       return function () {         targetFuc.apply( thisArg, argumentsArray.slice(1).concat( slice.call( arguments ) ) )       }     }     var f1 = function (x,y) { console.log( this, x, y ) }     f1.call( { a: 1 }, 6, 7 )     f1.apply( { a: 1 }, [ 6, 7 ] )     var f2 = function (x,y) { console.log( this, x, y ) }     f2.bind( { b:1 }, 8, 9 )()   

좋은 웹페이지 즐겨찾기