JavaScript instanceof [英] JavaScript instanceof

查看:86
本文介绍了JavaScript instanceof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能否告诉我为什么在下面的例子中超级的子实例是 false

Can you please tell my why in the example below sub instanceof Super is false?

function Super(){
    var obj = {
        prop1: "value1"
    };
    return obj;
}

var sub = new Super();
sub instanceof Super // false


推荐答案

因为它不是那种类型的实例 - 你已经返回了一个匿名对象。如果你这样写的话:

Because its not an instance of that type - you've returned an anonymous object. If you would have written it like this:

function Super(){
 this.prop1 = 'value1';   
}

var sub = new Super();
console.log(sub instanceof Super) // true

它可以按预期工作

这篇关于JavaScript instanceof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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