[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/apply [Back]  [Original]

Function.prototype.apply() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Function.prototype.apply()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

apply() this , ( ).

: call(), , call() , apply() .

In this article

fun.apply(thisArg, [argsArray])

thisArg

this, fun. , this , : , null undefined , .

argsArray

, , fun , null undefined, . ECMAScript 5 , . .

this . this , . apply() , .

apply call(), . . apply , , fun.apply(this, ['', '']), Array, , fun.apply(this, new Array('', '')).

argsArray arguments. arguments . . , , apply(). arguments . .

5- ECMAScript, , , length (0...length). , NodeList { 'length': 2, '0': '', '1': '' }.

: , Chrome 14 Internet Explorer 9, .

: apply()

apply() , Java. Function construct(), .

js
Function.prototype.construct = function (aArgs) {
  var oNew = Object.create(this.prototype);
  this.apply(oNew, aArgs);
  return oNew;
};

: Object.create(), , . :

js
Function.prototype.construct = function (aArgs) {
  var fConstructor = this,
    fNewConstr = function () {
      fConstructor.apply(this, aArgs);
    };
  fNewConstr.prototype = fConstructor.prototype;
  return new fNewConstr();
};

:

js
function MyConstructor() {
  for (var nProp = 0; nProp < arguments.length; nProp++) {
    this["property" + nProp] = arguments[nProp];
  }
}

var myArray = [4, ", !", false];
var myInstance = MyConstructor.construct(myArray);

alert(myInstance.property1); //  ', !'
alert(myInstance instanceof MyConstructor); //  'true'
alert(myInstance.constructor); //  'MyConstructor'

: Function.construct() ( Date, ). Function.prototype.bind() (, , , Date: [2012, 11, 4]; - : new (Function.prototype.bind.apply(Date, [null].concat([2012, 11, 4])))() , , , ).

: apply()

apply() , . Math.max()/Math.min() / .

js
/* /    */
var numbers = [5, 6, 2, 3, 7];

/*  apply  Math.min/Math.max */
var max = Math.max.apply(
  null,
  numbers,
); /*   Math.max(numbers[0], ...)
                                             Math.max(5, 6, ...) */
var min = Math.min.apply(null, numbers);

/*       */
((max = -Infinity), (min = +Infinity));

for (var i = 0; i < numbers.length; i++) {
  if (numbers[i] > max) {
    max = numbers[i];
  }
  if (numbers[i] < min) {
    min = numbers[i];
  }
}

: apply() JavaScript. (, ) (JavaScriptCore 65536), ( , ) . . , . ( : [ , , ], , apply() 5, 6, 2, 3, .) , : :

js
function minOfArray(arr) {
  var min = Infinity;
  var QUANTUM = 32768;

  for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
    var submin = Math.min.apply(null, arr.slice(i, Math.min(i + QUANTUM, len)));
    min = Math.min(submin, min);
  }

  return min;
}

var min = minOfArray([5, 6, 2, 3, 7]);

: apply() monkey-

apply() monkey- Firefox , JS . someobject.foo(), :

js
var originalfoo = someobject.foo;
someobject.foo = function () {
  //  -   
  console.log(arguments);
  //   ,        :
  originalfoo.apply(this, arguments);
  //  -   .
};

, , -, API, .on([event]..., , ).

Specification
ECMAScript 2027 LanguageSpecification
# sec-function.prototype.apply


Web Proxy Viewer  |  New URL  |  Original Page