带有事件处理程序的Javascript变量范围
这是关于JS变量作用域的第n个问题,但我通读了其余部分,并没有完全得到答案带有事件处理程序的Javascript变量范围,javascript,html,Javascript,Html,这是关于JS变量作用域的第n个问题,但我通读了其余部分,并没有完全得到答案 var notification = window.webkitNotifications.createNotification( 'image.png', // The image. 'New Post: ',// The title. 'New stuff'// The body. ); var itemLink = 'http://pathtothefile.com';
var notification = window.webkitNotifications.createNotification(
'image.png', // The image.
'New Post: ',// The title.
'New stuff'// The body.
);
var itemLink = 'http://pathtothefile.com';
notification.onclick = function(itemLink) {
window.focus();
window.open(itemLink,'_blank' );
this.cancel();
};
notification.show();
如何使全局作用域中定义的itemLink在onclick函数中工作?从函数中删除参数:
notification.onclick = function(itemLink) { // overrides itemLink in the global
// object.
固定代码:
notification.onclick = function() {
window.focus();
window.open(itemLink,'_blank' );
this.cancel();
};
从函数中删除参数:
notification.onclick = function(itemLink) { // overrides itemLink in the global
// object.
固定代码:
notification.onclick = function() {
window.focus();
window.open(itemLink,'_blank' );
this.cancel();
};
在名称冲突期间,局部变量优先。删除或重命名参数
itemLink
notification.onclick = function(something_else) {
//global itemLink should be accessible
};
在名称冲突期间,局部变量优先。删除或重命名参数
itemLink
notification.onclick = function(something_else) {
//global itemLink should be accessible
};
将其从参数列表中删除。它只是自动在那里,因为它是一个较低的作用域。从参数列表中删除它。它只是自动在那里,因为它是一个较低的范围。