为什么 Safari 中的范围缩减会破坏现有代码? [英] Why does scope reduction in Safari break existing code?

查看:48
本文介绍了为什么 Safari 中的范围缩减会破坏现有代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox 没有问题

Firefox has no problem with

var logger = window.console.log,  // breaks Safari but not Firefox

但 Safari 在我尝试使用 logger() 的第一个地方给出了类型错误.请注意下面的代码片段.

but Safari gives a Type Error the first place I try to use logger(). Note the snippet below.

我继续用 window.console.log() 替换了我所有的 logger() 以消除错误.

I went ahead and replaced all my logger()'s with window.console.log() to get rid of the error.

我不明白为什么 Safari 不喜欢它以及为什么它称之为类型错误.

I don't understand why Safari doesn't like it and why it calls it a type error.

错误

类型错误:类型错误

代码片段

/*log
**
**
**
*/
NS.log = function (arg) {
    if (window.console) {
        var logger = window.console.log,  // breaks Safari but not Firefox
            str1 = Object.prototype.toString.call(arg);
// ... snip

推荐答案

这应该有效:

var logger = window.console.log.bind(window.console)

这篇关于为什么 Safari 中的范围缩减会破坏现有代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆