Name

Function

function.apply(thisArg, argArray)

Die apply-Methode ruft eine funktion auf. Sie erwartet ein Objekt, das an this gebunden wird, sowie ein optionales Array von Argumenten. Die apply-Methode wird im apply-Aufrufmuster (Kapitel 4) verwendet:

Function.method('bind', function (that) {

// Gibt eine Funktion zurück, die diese Funktion aufruft,
// als wäre sie eine Methode dieses Objekts.

    var method = this,
        slice = Array.prototype.slice,
        args = slice.apply(arguments, [1]);
    return function (  ) {
        return method.apply(that,
            args.concat(slice.apply(arguments, [0])));
    };
});

var x = function (  ) {
    return this.value;
}.bind({value: 666});
alert(x(  )); // 666

Get Das Beste an JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.