javascript – 使用“this”作为函数?

我用一条我不理解的行继承了一些代码.
function updateQty () {
        obj.find('.inputAmount').html(qty);
        input.val(qty);

        $.each(change_actions,function() {
            this(qty);
        });
    }

.each函数中究竟发生了什么?我以前从未见过这种(var)这种方式.

解决方法

作者使用绑定设置他们自己的事件,因此change_actions很可能是订阅数量发生某些事情时运行的函数.

尝试这样的事情:

// initialize with a value
var actions = [
    function(x){ console.log('I see a new x: ' + x); }
];

// add actions "later"
actions.push(function(x){ console.log('Yup,a new x: ' + x); });

// Then execute them:
$.each(actions,function(){
  this(4);
});

// add another one still
actions.push(function(x){ console.log(x + ' looks new'); });

// re-iterate over them
// Then execute them:
$.each(actions,function(){
  this(5);
});

结果:

// first iteration (only 2 subscribed events)
[15:56:50.030] "I see a new x: 4"
[15:56:50.030] "Yup,a new x: 4"

// second iteration (Now we have 3,one was added later)
[15:56:50.030] "I see a new x: 5"
[15:56:50.030] "Yup,a new x: 5"
[15:56:50.030] "5 looks new"  // <-- new subscription

把它想象成click事件以及如何通过绑定到$(‘element’)来添加订阅.单击().每次发生点击时,都会触发任何订阅的事件.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...