当我运行这个简单的函数时我很感兴趣
$(window).resize(function() {
var that = $(this);
var widthValue = that.width();
console.log(widthValue + 'px');
});
当我开始调整浏览器窗口大小时它就起作用了。
但是当我这样做时
$(window).resize(function() {
var that = $(this);
var widthValue = that.width();
console.log(widthValue + 'px');
}).resize();
它的作用类似于load();
。我在最后添加了 resize()
。
这叫什么?不确定我理解为什么以及如何工作。
最佳答案
该技术称为链接。
它归结为最后返回 this
的函数,因此您可以通过将方法调用一个接一个地链接来调用同一对象的另一个方法。
var foo = {
count: 0,
up: function () { this.count++; return this; },
show: function () { alert(this.count); return this; }
}
foo.show().up().show().up().up().show();
在此特定示例中,resize
方法被重载。如果你给它一个函数参数,那么它会将该函数绑定(bind)为事件处理程序。如果您不带参数调用它,那么它将触发该事件。
关于javascript - 这个 JavaScript 函数方法叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19226551/