【问题标题】:Can a variable be both an object and undefined at the same time?变量可以同时是对象和未定义的吗?
【发布时间】:2014-05-04 17:42:49
【问题描述】:

我正在使用 jasmine 的 spyOn 函数模拟 chrome.storage,如下所示:

this.fakeChromeStorage = {};
chrome = { storage: { sync: {get: function() {}, set: function() {}}}};
spyOn(chrome.storage.sync, 'get').and.callFake(function(key, callback) {
    callback();
});

但是当我的应用程序调用时:

console.info(chrome.storage);
console.info(typeof chrome.storage);
console.info(chrome.storage.type);
console.info(typeof chrome.storage.type);

打印出来:

[object Object]
object
undefined
undefined

这怎么可能?

【问题讨论】:

  • 因为chrome.storage 是一个具有未定义type 属性的对象?我不确定这有什么令人困惑的地方。
  • 它令人困惑,因为我认为.type 是你表达typeof 的方式
  • 一点也不。 typeof 是唯一计算类型的东西。 .type 这里只是一个随机属性。

标签: javascript gruntjs jasmine phantomjs


【解决方案1】:

是的,chrome.storage 是一个对象,chrome.storage.type 是对象 chrome.storage 的一个属性,它不存在,因此将是未定义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多