在回调中访问'this'应该引用我的数组

[英]Accessing 'this' inside the callback should reference my array


im kinda new at javascript so i have an function which is given an array and it filters its elements from the arguments it is passed. What i need though is to make 'this' inside the callback function to refference at my originalArray so that console.log(this); would acctually print [1,2,3]. Have in mind that i acctually need to this in these lines because i can only edit all those lines not add some more.

我有点新的javascript所以我有一个函数,给定一个数组,它从传递的参数中过滤其元素。我需要的是在回调函数中使'this'在我的originalArray中进行refference,以便console.log(this);会实际打印[1,2,3]。请记住,我在这些行中实际需要这个,因为我只能编辑所有这些行而不是添加更多。

Here is my code:

这是我的代码:

function makeMultiFilter(array) {
    // What we track
    // TO BE COMPLETED
   // TO BE COMPLETED
    return (function arrayFilterer(pred, callback) {


        // If filter not a function return current Array
        // TO BE COMPLETED
            // TO BE COMPLETED
        // Filter out things
        // TO BE COMPLETED

        // If callback is a function, execute callback
        // TO BE COMPLETED
        // TO BE COMPLETED
            return arrayFilterer;
    });
}

And the test code:

和测试代码:

var arrayFilterer = makeMultiFilter([1, 2, 3]);
// call arrayFilterer to filter out all the numbers not equal to 2
arrayFilterer(function(elem) {

    return elem != 2; // check if element is not equal to 2
}, function(currentArray) {
    console.log(this); // prints [1,2 3]
    console.log(currentArray);
}); // prints [1, 3]`

1 个解决方案

#1


2  

You can use call to execute a function in a given this context:

您可以使用call在给定的上下文中执行函数:

var callback = function() {
  console.log(this);
  console.log(arguments);
};

// Using call
callback.call([1,2,3], "A", "B", "C");

// Using apply
callback.apply([1,2,3], ["A", "B", "C"]);

// Using bind:
//  creates a new function bound to [1,2,3]
callback.bind([1,2,3])("A", "B", "C");
callback.bind([1,2,3], "A")("B", "C"); // etc.

I guess that in your example code, that would mean that instead of doing callback(), you'd use callback.call(originalArray):

我想在你的示例代码中,这意味着你不使用callback(),而是使用callback.call(originalArray):

function makeMultiFilter(array) {
    // What we track
    var originalArray = array;
    var currentArray = originalArray;
    return (function arrayFilterer(pred, callback) {
        // If filter not a function return current Array
        if (typeof pred !== "function")
            return currentArray;
        // Filter out things
        currentArray = currentArray.filter(pred);

        // If callback is a function, execute callback
        if (typeof callback === "function")
            callback.call(originalArray, currentArray);
            return arrayFilterer;
    });
}

var arrayFilterer = makeMultiFilter([1, 2, 3]);

// call arrayFilterer to filter out all the numbers not equal to 2
arrayFilterer(function(elem) {
    return elem != 2; // check if element is not equal to 2
}, function(currentArray) {
    console.log(this); // prints [1,2 3]
    console.log(currentArray); // prints [1,3]
}); // prints [1, 3]`


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/10/24/a829879bb725c9b921344e53df12e1e8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com