Binding applies to a variable

Does anyone know why in javascript this works

m = Math.max
m.apply(null, [1,2,3])

but is it not so?

m = Math.max.apply
m(null, [1,2,3])

It throws an exception:

TypeError: Function.prototype.apply was called on undefined, which is undefined, not a function

+4
source share
2 answers

According to spec

  • If IsCallable (func) is false, throw a TypeError exception.

funcis the object on which the method is invoked apply.

applyallows you to specify the context functionlater, which undefinedin your case, since the Function (which should be specified in the arguments) mhas no context .

Since the arguments

TypeError: Function.prototype.apply undefined, undefined,

,

1:

m = Math.max.apply.bind(this)
m(this, [1,2,3]) 

TypeError: Function.prototype.apply , ,

2:

m = Math.max.apply.bind(null)
m(this, [1,2,3])

TypeError: Function.prototype.apply null, ,

3: ( , )

m = Math.max.apply.bind(function(){})
m(this, [1,2,3])

undefined

4: (, )

m = Math.max.apply.bind(Math.max)
m(this, [1,2,3])

3

+4

, , Javascript. Javascript , , . , , , this .

apply - , , , Math.max. apply . m apply, , , apply , .

+1

Source: https://habr.com/ru/post/1656664/


All Articles