吉林快三今天直播—官方网址22270.COM_鎺㈢储 Reflect.apply 涓 Function.prototype.apply 鐨勫尯鍒

吉林快三今天直播—官方网址22270.COM_鎺㈢储 Reflect.apply 涓 Function.prototype.apply 鐨勫尯鍒

浼楁墍鍛ㄧ煡锛 ES6 鏂板浜嗕竴涓叏灞銆佸唴寤恒佷笉鍙瀯閫犵殑 Reflect 瀵硅薄锛屽苟鎻愪緵浜嗗叾涓嬩竴绯诲垪鍙鎷︽埅鐨勬搷浣滄柟娉曘傚叾涓竴涓究鏄 Reflect.apply() 浜嗐備笅闈㈡帰绌朵笅瀹冧笌浼犵粺 ES5 鐨 Function.prototype.apply()吉林快三今天直播—官方网址22270.COM涔嬮棿鏈変粈涔堝紓鍚屻

鍑芥暟绛惧悕

MDN 涓婁袱鑰呯殑鍑芥暟绛惧悕鍒嗗埆濡備笅锛

Reflect.apply(target, thisArgument, argumentsList)
function.apply(thisArg, [argsArray])

鑰 TypeScript 瀹氫箟鐨勫嚱鏁扮鍚嶅垯鍒嗗埆濡備笅锛

declare namespace Reflect {
    function apply(target: Function, thisArgument: any, argumentsList: ArrayLike<any>): any;
}
interface Function {
    apply(this: Function, thisArg: any, argArray?: any): any;
}

瀹冧滑閮芥帴鍙椾竴涓彁渚涚粰琚皟鐢ㄥ嚱鏁扮殑 this 鍙傛暟鍜屼竴涓弬鏁版暟缁勶紙鎴栦竴涓被鏁扮粍瀵硅薄锛 array-like object 锛夈

鍙夊弬鏁

鍙互鏈鐩磋鐪嬪埌鐨勬槸锛 function.apply() 缁欏嚱鏁扮殑绗簩涓紶鍙傘屽弬鏁版暟缁勩嶆槸鍙夌殑锛屽綋涓嶉渶瑕佷紶閫掑弬鏁扮粰琚皟鐢ㄧ殑鍑芥暟鏃讹紝鍙互涓嶄紶鎴栦紶閫 nullundefined 鍊笺傝岀敱浜 function.apply() 鍙湁涓や釜鍙傛暟锛屾墍浠ュ疄璺典腑杩炵涓涓弬鏁颁篃鍙互涓璧蜂笉浼狅紝鍘熺悊涓婂彲浠ュ湪瀹炵幇涓幏寰 undefined 鍊笺

(function () { console.log('test1') }).apply()
// test1
(function () { console.log('test2') }).apply(undefined, [])
// test2
(function () { console.log('test3') }).apply(undefined, {})
// test3
(function (text) { console.log(text) }).apply(undefined, ['test4'])
// test4

Reflect.apply() 鍒欒姹傛墍鏈夊弬鏁伴兘蹇呬紶锛屽鏋滃笇鏈涗笉浼犲弬鏁扮粰琚皟鐢ㄧ殑鍑芥暟锛屽垯蹇呴』濉竴涓┖鏁扮粍鎴栬呯┖鐨勭被鏁扮粍瀵硅薄锛堢函 JavaScript 涓嬬┖瀵硅薄涔熷彲浠ワ紝鑻ユ槸 TypeScript 鍒欓渶甯︿笂 length: 0吉林快三今天直播—官方网址22270.COM鐨勯敭鍊煎浠ラ氳繃绫诲瀷妫鏌ワ級銆

Reflect.apply(function () { console.log('test1') }, undefined)
// Thrown:
// TypeError: CreateListFromArrayLike called on non-object
Reflect.apply(function () { console.log('test2') }, undefined, [])
// test2
Reflect.apply(function () { console.log('test3') }, undefined, {})
// test3
Reflect.apply(function (text) { console.log(text) }, undefined, ['test4'])
// test4

闈炰弗鏍兼ā寮

鐢辨枃妗e彲鐭ワ紝 function.apply() 鍦ㄩ潪涓ユ牸妯″紡涓 thisArg 鍙傛暟鍙樼幇浼氭湁鎵涓嶅悓锛岃嫢瀹冪殑鍊兼槸 nullundefined 锛屽垯浼氳鑷姩鏇挎崲涓哄叏灞瀵硅薄锛堟祻瑙堝櫒涓嬩负 window 锛夛紝鑰屽熀鏈暟鎹被鍨嬪煎垯浼氳鑷姩鍖呰锛堝瀛楅潰閲 1 鐨勫寘瑁呭肩瓑浠蜂簬 Number(1) 锛夈

Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed. This argument is not optional

(function () { console.log(this) }).apply(null)
// Window {...}
(function () { console.log(this) }).apply(1)
// Number { [[PrimitiveValue]]: 1 }
(function () { console.log(this) }).apply(true)
// Boolean { [[PrimitiveValue]]: true }
'use strict';
(function () { console.log(this) }).apply(null)
// null
(function () { console.log(this) }).apply(1)
// 1
(function () { console.log(this) }).apply(true)
// true

浣嗙粡杩囨祴璇曪紝鍙戠幇涓婅堪璇ラ潪涓ユ牸妯″紡涓嬬殑琛屼负瀵逛簬 Reflect.apply()吉林快三今天直播—官方网址22270.COM涔熸槸鏈夋晥鐨勶紝鍙槸 MDN 鏂囨。娌℃湁鍚屾牱鍐欐槑杩欎竴鐐广

寮傚父澶勭悊

Reflect.apply 鍙浣滃 Function.prototype.apply 鐨勫皝瑁咃紝涓浜涘紓甯稿垽鏂槸涓鏍风殑銆傚浼犻掔殑鐩爣鍑芥暟 target 瀹為檯涓婁笉鍙皟鐢ㄣ佷笉鏄竴涓嚱鏁扮瓑绛夛紝閮戒細瑙﹀彂寮傚父銆吉林快三今天直播—官方网址22270.COM浣嗗紓甯哥殑琛ㄧ幇鍗村彲鑳芥槸涓嶄竴鏍风殑銆

濡傛垜浠悜 target 鍙傛暟浼犻掍竴涓璞¤岄潪鍑芥暟锛屽簲褰撹Е鍙戝紓甯搞

Function.prototype.apply() 鎶涘嚭鐨勫紓甯歌涔変笉鏄庯紝鐩磋瘧鏄 .call 涓嶆槸涓涓嚱鏁帮紝浣嗗鏋滄垜浠紶閫掍竴涓纭彲璋冪敤鐨勫嚱鏁板璞★紝鍒欎笉浼氭姤閿欙紝璁╀汉杩锋儜 Function.prototype.apply 涓嬪埌搴曟湁娌℃湁 call 灞炴э紵

Function.prototype.apply.call()
// Thrown:
// TypeError: Function.prototype.apply.call is not a function
Function.prototype.apply.call(console)
// Thrown:
// TypeError: Function.prototype.apply.call is not a function
Function.prototype.apply.call(console.log)
///- 杈撳嚭涓虹┖锛岀鍚堥鏈

Function.prototype.apply() 鎶涘嚭鐨勫紓甯稿叿鏈夋涔夛紝鍚屾牱鏄粰 target 鍙傛暟浼犻掍笉鍙皟鐢ㄧ殑瀵硅薄锛屽鏋滆ˉ榻愪簡绗簩銆佺涓変釜鍙傛暟锛屽垯鎶涘嚭鐨勫紓甯告弿杩颁笌涓婅堪瀹屽叏涓嶅悓锛

Function.prototype.apply.call(console, null, [])
// Thrown:
// TypeError: Function.prototype.apply was called on #<Object>, which is a object and not a function
Function.prototype.apply.call([], null, [])
// Thrown:
// TypeError: Function.prototype.apply was called on [object Array], which is a object and not a function
Function.prototype.apply.call('', null, [])
// Thrown:
// TypeError: Function.prototype.apply was called on , which is a string and not a function

涓嶈繃 Reflect.apply() 瀵逛簬鍙紶閫掍竴涓笉鍙皟鐢ㄥ璞$殑寮傚父锛屾槸涓 Function.prototype.apply() 鍏ㄥ弬鏁扮殑寮傚父鏄竴鏍风殑锛

Reflect.apply(console)
// Thrown:
// TypeError: Function.prototype.apply was called on #<Object>, which is a object and not a function

鑰屽鏋滀紶閫掍簡姝g‘鍙皟鐢ㄧ殑鍑芥暟锛屾墠浼氬幓鏍¢獙绗笁涓弬鏁版暟缁勭殑鍙傛暟锛涜繖涔熻鏄 Reflect.apply() 鐨勫弬鏁版牎楠屾槸鏈夐『搴忕殑锛

Reflect.apply(console.log)
// Thrown:
// TypeError: CreateListFromArrayLike called on non-object

瀹為檯浣跨敤

铏界劧鐩墠娌℃湁鍦 Proxy 浠ュ鐨勫満鏅湅鍒版洿澶氱殑浣跨敤妗堜緥锛屼絾鐩镐俊鍦ㄥ吋瀹规ч棶棰橀愭笎鍙樺緱涓嶆槸闂鐨勬椂鍊欙紝浣跨敤鐜囦細寰楀埌閫愭笎涓婂崌銆

鎴戜滑鍙互鍙戠幇 ES6 Reflect.apply() 鐨勫舰寮忕浉杈冧簬浼犵粺 ES5 鐨勭敤娉曪紝浼氭樉寰楁洿鐩磋銆佹槗璇讳簡锛岃浜烘洿瀹规槗鐪嬪嚭锛屼竴琛屼唬鐮佸笇鏈涗娇鐢ㄥ摢涓嚱鏁帮紝鎵ц棰勬湡鐨勮涓恒

// ES5
Function.prototype.apply.call(<Function>, undefined, [...])
<Function>.apply(undefined, [...])
// ES6
Reflect.apply(<Function>, undefined, [...])

鎴戜滑閫夋嫨甯哥敤鐨 Object.prototype.toString 姣旇緝鐪嬬湅锛

Object.prototype.toString.apply(/ /)
// '[object RegExp]'
Reflect.apply(Object.prototype.toString, / /, [])
// '[object RegExp]'

鍙兘鏈変汉浼氫笉鍚屾剰锛岃繖涓嶆槸鍐欏緱鏇撮暱銆佹洿楹荤儲浜嗗悧锛熷叧浜庤繖鐐癸紝瑙佷粊瑙佹櫤锛屽浜庡崟涓鍑芥暟鐨勯噸澶嶈皟鐢紝纭疄鏄墦鐨勪唬鐮佹洿澶氫簡锛涘浜庨渶瑕佺伒娲讳娇鐢ㄧ殑鍦烘櫙锛屼細鏇寸鍚堝嚱鏁板紡鐨勯鏍硷紝鍙渶鎸囧畾鍑芥暟瀵硅薄銆佷紶閫掑弬鏁帮紝鍗冲彲鑾峰緱棰勬湡鐨勭粨鏋溿

浣嗘槸瀵逛簬杩欎釜妗堜緥鏉ヨ锛屽彲鑳借繕浼氭湁涓鐐瑰皬闂锛氭瘡娆¤皟鐢ㄩ兘闇瑕佸垱寤轰竴涓柊鐨勭┖鏁扮粍锛佸敖绠$幇鍦ㄥ鏁拌澶囨ц兘瓒冲濂斤紝绋嬪簭鍛樹笉闇棰濆鑰冭檻杩欑偣鎹熻楋紝浣嗘槸瀵逛簬楂樻ц兘銆佸紩鎿庡張娌℃湁浼樺寲鐨勫満鏅紝鍏堝垱寤轰竴涓彲閲嶅浣跨敤鐨勭┖鏁扮粍鍙兘浼氭洿濂斤細

const EmptyArgs = []

function getType(obj) {
    return Reflect.apply(
        Object.prototype.toString,
        obj,
        EmptyArgs
    )
}

鍙︿竴涓皟鐢 String.fromCharCode() 鐨勫満鏅彲浠ュ仛浠g爜涓瓧绗︿覆鐨勬贩娣嗭細

Reflect.apply(
    String.fromCharCode,
    undefined,
    [104, 101, 108, 108,
     111,  32, 119, 111,
     114, 108, 100,  33]
)
// 'hello world!'

瀵逛簬鍙紶澶氫釜鍙傛暟鐨勫嚱鏁板 Math.max() 绛夊彲鑳戒細鏇存湁鐢紝濡傦細

const arr = [1, 1, 2, 3, 5, 8]
Reflect.apply(Math.max, undefined, arr)
// 8
Function.prototype.apply.call(Math.max, undefined, arr)
// 8
Math.max.apply(undefined, arr)
// 8

浣嗙敱浜庤瑷鏍囧噯瑙勮寖娌℃湁鎸囧畾鏈澶у弬鏁颁釜鏁帮紝濡傛灉浼犲叆澶ぇ鐨勬暟缁勭殑璇濅篃鍙兘鎶ヨ秴杩囨爤澶у皬鐨勯敊璇傝繖涓ぇ灏忓洜骞冲彴鍜屽紩鎿庤屽紓锛屽 PC 绔 node.js 鍙互杈惧埌寰堝ぇ鐨勫ぇ灏忥紝鑰屾墜鏈虹鐨 JSC 鍙兘灏变細闄愬埗鍒 65536 绛夈

const arr = new Array(Math.floor(2**18)).fill(0)
// [
//   0, 0, 0, 0,
//   ... 262140 more items
// ]
Reflect.apply(Math.max, null, arr)
// Thrown:
// RangeError: Maximum call stack size exceeded

鎬荤粨

ES6 鏂版爣鍑嗘彁渚涚殑 Reflect.apply() 鏇磋鏁存槗鐢紝瀹冩湁濡備笅鐗圭偣锛

  1. 鐩磋鏄撹锛屽皢琚皟鐢ㄥ嚱鏁版斁鍦ㄥ弬鏁颁腑锛岃创杩戝嚱鏁板紡椋庢牸锛
  2. 寮傚父澶勭悊鍏锋湁涓鑷存э紝鏃犳涔夛紱
  3. 鎵鏈夊弬鏁板繀浼狅紝缂栬瘧鏈熼敊璇鏌ュ拰绫诲瀷鎺ㄦ柇鏇村弸濂姐

濡備粖 Vue.js 3 涔熷湪鍏跺搷搴斿紡绯荤粺涓ぇ閲忎娇鐢 Proxy 鍜 Reflect 浜嗭紝鏈熷緟涓嶄箙鐨勫皢鏉 Reflect 浼氬湪鍓嶇涓栫晫涓ぇ鏀惧紓褰╋紒

posted @ 2019-12-03 10:51  BlackStorm  闃呰(...)  璇勮(... 缂栬緫 鏀惰棌