如果有的话,Javascript的"typeof"会返回错误的类型吗?

 我是你的特效 发布于 2023-01-30 13:06

我试图通过别人的代码工作,并发现一些奇怪的事情.我在程序的一部分中找到了以下代码:

if(typeof func == "function" || (typeof func == "object" && typeof func.document == "undefined"))
{
    func();
}

这对我没有意义,因为我不确定这样的事件何时会发生?typeof返回错误的类型?或者非功能对象是否可以调用?

代码很乱,所以我无法找到这个特定检查可能成功的例子.我应该注意,代码可能超过10年.我收到的声明是它是必需的,因为有时,当代码最初编写时,typeof没有将函数作为类型返回.

此外,这个检查是必需的,因为有时func会传递一个窗口对象(?),所以有必要确保它不是一个窗口对象.

1 个回答
  • 记住:typeof是文字.

    typeof undefined === "undefined"
    typeof 5 === "number"
    typeof true === "boolean"
    typeof "" === "string"
    typeof {} === "object"
    typeof [] === "object"
    typeof function { } === "function"
    
    // null is a weird exception:
    typeof null === "object"
    

    instanceof区分什么东西是"函数"(注意对type "object"函数Object本身的字符串instanceof检查的typeof检查:

    {} instanceof Object
    
    function {} instanceof Object
    function {} instanceof Function
    
    [] instanceof Object
    [] instanceof Array
    

    如你所见,typeof说=== "function"!== "object":这可能会产生误导,因为函数也是一个对象.这是instanceof发挥作用的时候.

    现在,当您编写构造函数时,构造的对象也是您的函数的实例:

    // the constructor
    function Example { }
    
    // the object
    var ex = new Example();
    
    typeof Example === "function"
    typeof ex === "object"
    ex instanceof Object
    ex instanceof Example
    

    使用原型你也可以扩展链,其中typeof总是说对象:

    function DerivedExample { }
    
    DerivedExample.prototype = new Example();
    // DerivedExample.prototype.constructor = DerivedExample;
    
    var ex1 = new DerivedExample();
    
    typeof DerivedExample === "function"
    typeof ex1 === "object"
    ex1 instanceof Object
    ex1 instanceof Example
    ex1 instanceof DerivedExample
    

    所以这是所有关于JavaScript typeof和也instanceof.希望它澄清正在发生的事情.


    还有一件事要知道(建议不要在生产代码中使用,但我在CoffeeScript源代码中也看到过):

    typeof new Number(5) === "object"
    new Number(5) instanceof Object
    new Number(5) instanceof Number
    
    typeof new Boolean("true") === "object"
    new Boolean("true") instanceof Object
    new Boolean("true") instanceof Boolean
    
    typeof new String("") === "object"
    new String("") instanceof Object
    new String("") instanceof String
    

    关于函数和窗口对象:

    每个独立函数都将在窗口的上下文中调用.
    (除非)当你"use strict""指令"时,将使用undefined上下文调用独立函数.

    function world() {
        // non-strict: this === window
        // use-strict: this === undefined
    }
    world();
    

    现在,当您将函数编写为对象的成员时,函数的上下文是对象:

    var hello = {
        world: function () {
            // this === hello
        }
    };
    hello.world();
    

    对于在新构造的对象的上下文中调用的原型函数也是如此:

    function Hello() { }
    
    Hello.prototype.world = function () {
        // this instanceof Hello
        // this will be === ex
    };
    
    var ex = new Hello();
    ex.world();
    

    至少,您可以使用call和更改任何函数的上下文apply:

    var ex = { };
    
    function Example() {
        // (1) non-strict: this === window
        // (1) use-strict: this === undefined
        // (2) this === ex
    }
    
    Example(); // (1)
    Example.call(ex);  // (2)
    Example.apply(ex); // (2)
    

    2023-01-30 13:10 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有