Javascript window.onblur和console.log带有绑定的部分应用程序

Javascript window.onblur和console.log带有绑定的部分应用程序,javascript,window,console.log,onblur,Javascript,Window,Console.log,Onblur,我读了一些javascript文章,我发现了 基本上,在回答中,海报上说你可以 window.onblur = myBlurFunction 仅当myBlurFunction是一个不需要向其传递任何参数的函数时 我正要评论说,可以使用bind对需要参数的函数执行部分应用程序,但当我尝试时 var myBlurFunction = console.log.bind(console,'blur'); window.onblur = myBlurFunction; 模糊窗口并没有打印字符串“bl

我读了一些javascript文章,我发现了

基本上,在回答中,海报上说你可以

window.onblur = myBlurFunction
仅当
myBlurFunction
是一个不需要向其传递任何参数的函数时

我正要评论说,可以使用
bind
对需要参数的函数执行部分应用程序,但当我尝试时

var myBlurFunction = console.log.bind(console,'blur'); 
window.onblur = myBlurFunction;
模糊窗口并没有打印字符串“blur”,而是打印了似乎是模糊对象的内容

模糊{目标:窗口→ 窗口不工作

有人知道为什么这种方法不起作用吗


我真正想问的是,为什么事件处理函数将事件作为参数给定

window.onblur = function(event){console.log(event)}
我从未见过任何提到或解释事件参数的文档

此外,如何覆盖绑定参数?通常,一旦值绑定到函数参数,任何附加参数都将分配给后续参数:

var f = function(arg1,arg2){console.log(arg1,arg2)}; 
g = f.bind(null,1);
g();            // 1 undefined
g(2);           // 1 2
g.call(null,2); // 1 2
引述:

bind()方法创建一个新函数,在调用该函数时,将其
this
关键字设置为提供的值,并在调用新函数时提供的任何参数之前提供给定的参数序列

让我们将这两个概念分开,然后在示例中一起解释它们


更改此上下文中的 这是MDN页面中示例的改编(我保证很简单!)

由此,我们得出结论,将
obj
作为参数传递会改变该引用的上下文

在你的例子中,你正在做这样的事情:

console.log.bind(console);  // The second argument doesn't matter for now
console.log.bind(console, "blur");
因此,您告诉
console.log
的任何实例都是对
console
上下文的引用。我认为这很好,不会造成太大损害


前置参数 同样,根据MDN页面示例进行调整:

function list() {
    // Simply convers the arguments list into an Array, then returns it
    return Array.prototype.slice.call(arguments);
}

// Example usage
list(1, 2, 3);
//=> [1, 2, 3]

// Using 'bind' to prepend to (append to the start of) the arguments list
// Note that, because 'this' context doesn't matter, the first argument is null
var betterList = list.bind(null, 98);

// Passing no arguments, it returns an array with only 98
// This is similar to '[98].concat([])'
betterList();
//=> [98]

// Passing arguments, it appends 98 to the start of the array
// Again, this is similar to '[98].concat([1,2,3])'
betterList(1, 2, 3);
// [98, 1, 2, 3]

// The parameters can go on indefinitely. They will all be added to the start of the arguments list in order
var bestList = list.bind(null, 98, 99, 100);

bestList(1, 2, 3);
//=> [98, 99, 100, 1, 2, 3]
函数
list
将类似数组的对象(包含传递给函数的所有参数)转换为实际数组

通过使用
bind()
,我们将值附加到参数列表的开头,这样一来,对于函数来说,似乎它们一开始就已经以这种方式传递了

您的代码如下所示:

console.log.bind(console);  // The second argument doesn't matter for now
console.log.bind(console, "blur");
忽略第一个参数,您将使用
“blur”
预先添加发送到
控制台.log
(在本例中为事件响应)的参数。这也没有什么害处,只是不是很有用


最后的想法 这里是我处理参数的屏幕截图。第一个参数,表示
这个
的上下文,被设置为
null
,就像上面的例子一样,因为它在这里实际上并不重要。之后我传递了一长串参数,作为
onblur
事件响应的前置

正如你所看到的,尽管我在回复中添加了一些东西,但(不是一个模糊的对象!哈哈)仍然存在

这就是为什么它“不”工作。它以自己的方式工作。这可能不是你所期望的

您仍然可以选择链接问题中提出的方法,例如

window.onblur = () => console.log("blur");
不太复杂的解决方案,并实际实现您期望的功能

引用以下内容:

bind()方法创建一个新函数,在调用该函数时,将其
this
关键字设置为提供的值,并在调用新函数时提供的任何参数之前提供给定的参数序列

让我们将这两个概念分开,然后在示例中一起解释它们


更改此上下文中的 这是MDN页面中示例的改编(我保证很简单!)

由此,我们得出结论,将
obj
作为参数传递会改变该
引用的上下文

在你的例子中,你正在做这样的事情:

console.log.bind(console);  // The second argument doesn't matter for now
console.log.bind(console, "blur");
因此,您告诉
console.log
的任何实例都是对
console
上下文的引用。我认为这很好,不会造成太大损害


前置参数 同样,根据MDN页面示例进行调整:

function list() {
    // Simply convers the arguments list into an Array, then returns it
    return Array.prototype.slice.call(arguments);
}

// Example usage
list(1, 2, 3);
//=> [1, 2, 3]

// Using 'bind' to prepend to (append to the start of) the arguments list
// Note that, because 'this' context doesn't matter, the first argument is null
var betterList = list.bind(null, 98);

// Passing no arguments, it returns an array with only 98
// This is similar to '[98].concat([])'
betterList();
//=> [98]

// Passing arguments, it appends 98 to the start of the array
// Again, this is similar to '[98].concat([1,2,3])'
betterList(1, 2, 3);
// [98, 1, 2, 3]

// The parameters can go on indefinitely. They will all be added to the start of the arguments list in order
var bestList = list.bind(null, 98, 99, 100);

bestList(1, 2, 3);
//=> [98, 99, 100, 1, 2, 3]
函数
list
将类似数组的对象(包含传递给函数的所有参数)转换为实际数组

通过使用
bind()
,我们将值附加到参数列表的开头,这样一来,对于函数来说,似乎它们一开始就已经以这种方式传递了

您的代码如下所示:

console.log.bind(console);  // The second argument doesn't matter for now
console.log.bind(console, "blur");
忽略第一个参数,您将使用
“blur”
预先添加发送到
控制台.log
(在本例中为事件响应)的参数。这也没有什么害处,只是不是很有用


最后的想法 这里是我处理参数的屏幕截图。第一个参数,表示
这个
的上下文,被设置为
null
,就像上面的例子一样,因为它在这里实际上并不重要。之后我传递了一长串参数,作为
onblur
事件响应的前置

正如你所看到的,尽管我在回复中添加了一些东西,但(不是一个模糊的对象!哈哈)仍然存在

这就是为什么它“不”工作。它以自己的方式工作。这可能不是你所期望的

您仍然可以选择链接问题中提出的方法,例如

window.onblur = () => console.log("blur");
不太复杂的解决方案,并实际实现您期望的效果