【问题标题】:Understanding closure and scope了解闭包和范围
【发布时间】:2012-01-27 15:36:10
【问题描述】:

出于某种原因(可能是因为我不懂闭包)函数inResult 总是返回false 并且循环永远不会执行。当然,我确信result contains 具有正确的属性。

    function hasId() {return $(this).prop('id');}
    function inResult(res) { return res.hasOwnProperty($(this).prop('id'));}

    $.ajax({
        url : opt.url,
        data : $.extend(true, opt.data, {ids: ids}),
        context : this, // A collection of elements
        type : 'POST',
        dataType : 'json',
        success : function(result) {

            // Filter elements with id and with a property in result named "id"
            this.filter(hasId).filter(inResult(result)).each(function() {
                console.log($(this).prop('id'));
            });

        }
    });

编辑:工作代码解决方案(感谢 Šime Vidas 为我指明了正确的方向):

// Use closures to change the context later
var hasId    = function() { return $(this).prop('id'); };
var inResult = function(res) { return res.hasOwnProperty($(this).prop('id')); };

$.ajax({
    url : opt.url,
    data : $.extend(true, opt.data, {ids: ids}),
    context : this, // A collection of elements
    type : 'POST',
    dataType : 'json',
    success : function(result) {

        // Filter elements with id and with a property in result named "id"
        var filtered = this.filter(function() {
            // Note the context switch and result parameter passing
            return hasId.call(this) && isBinded.call(this, result);
        });

        filtered.each(function() { console.log($(this).prop('id')); });

    }
});

【问题讨论】:

  • this.id 工作正常。不需要prop()
  • @ŠimeVidas 谢谢你的提示。
  • 闭包不是 jQuery 特有的,它们是 JavaScript 特性之一。
  • @zizozu 你说得对,感谢编辑。

标签: javascript jquery closures


【解决方案1】:

试试这个:

this.filter( hasId ).filter( function () {
    return inResult( result );
}).each( function () {
    console.log( this.id );
});

在您的代码中,.filter(inResult(result)) 不起作用,因为您正在立即调用 inResult 并将该调用的 result(这是一个布尔值)传递给 filter() ,这不适用于布尔值。


你也可以这样做:

var keys = Object.keys( result );

var filtered = this.filter( function () {
    return this.id && keys.indexOf( this.id ) > -1;
});

Object.keys( result )result 返回一个包含所有自有属性名称的数组。

【讨论】:

  • filter() 可以与返回布尔值的函数一起使用,所以我认为我的没有问题..
  • @Gremo 但是您没有将函数传递给filter()。您正在向它传递一个布尔值。 filter( true ) 不起作用。你必须向它传递一个函数。
  • 知道了!所以唯一的方法是将 che 调用包装成一个新的function?猜猜为什么 hasId 它正在工作......
  • @Gremo 有bind 函数方法,所以.filter( inResult.bind( this, result ) ) 也应该可以工作。 (但是,bind 没有在 IE8 和 Safari 中实现,因此您必须将 ES5-shim 包含到您的网页中。)
  • 但是inResult 中的关键字this 仍将引用窗口对象...
猜你喜欢
  • 2013-01-23
  • 2011-10-07
  • 2019-08-20
  • 2018-02-04
  • 1970-01-01
  • 2021-02-03
  • 2016-04-23
  • 2015-11-26
相关资源
最近更新 更多