How to get a JS js object type name / determine the type of object

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

The following excerpt from segmentfault address

Answer 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 都是可以更改的,所以这个也不是很稳当,自己写代码的时候需要小心。

Answer 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*/

Guess you like

Origin www.cnblogs.com/marvelousone/p/11323081.html