zoukankan      html  css  js  c++  java
  • JS 如何获取一个js对象的类型名称/判断对象类型

    // let key = Object.keys(pickedFeature)[0];
    // if(key==="_content"){    //pickedFeature._content==='Batched3DModel3DTileContent'
    // let pickFeaObjClsName = pickedFeature.constructor.name;
    if(pickedFeature instanceof Cesium.Cesium3DTileFeature)
    

    以下摘自segmentfault地址

    回答1

    //第1种情况是内置对象,
    var a = new Date()
    var name = Object.prototype.toString.call(a).match(/[object (.*?)]/)[1]
    //第2种情况就像 @iSayme 说的那样
    function Foo() {
       var f = new Foo()
    }
    var name = f.constructor.name
    第 3 种情况,构造是匿名函数
    var Foo = function() {}
    var f = new Foo()
    var name = f.constructor.name    // 得到 "",这种情况是取不到名字的
    //很多都是第3种情况,没别的办法,可以使用类构建器,在定义类的时候设置一个名称
    
    var Foo = function() {}
    Foo.className = "Foo"
    var f = new Foo()
    var name = f.constructor.className
    //不过由于 constructor 都是可以更改的,所以这个也不是很稳当,自己写代码的时候需要小心。
    

    回答2

    var Person = function(){};
    var Male = function(){};
    Male.prototype = new Person(); //类似于继承,Male继承于Person
    
    var p = new Person();
    var m = new Male();
    console.log(p instanceof Person);  /*print true*/
    console.log(m instanceof Male);    /*print true*/
    console.log(m instanceof Person);  /*print true*/
    console.log(p instanceof Male);    /*print false*/
    
    var x = {};
    console.log(x instanceof Person);  /*print false*/
    console.log(x instanceof Male);    /*print false*/
    

  • 相关阅读:
    React Native 项目常用第三方组件汇总
    react-native-swiper的Github地址
    Navicat for MySQL 使用
    maven项目创7 配置分页插件
    rander()函数执行条件
    RN生命周期
    react-native连接夜神模拟器
    让PHP7达到最高性能的几个Tips
    php安装
    被swoole坑哭的PHP程序员
  • 原文地址:https://www.cnblogs.com/marvelousone/p/11323081.html
Copyright © 2011-2022 走看看