Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有事件处理程序的Javascript变量范围_Javascript_Html - Fatal编程技术网

带有事件处理程序的Javascript变量范围

带有事件处理程序的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';

这是关于JS变量作用域的第n个问题,但我通读了其余部分,并没有完全得到答案

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
};

将其从参数列表中删除。它只是自动在那里,因为它是一个较低的作用域。从参数列表中删除它。它只是自动在那里,因为它是一个较低的范围。