Chrome控制台和Javascript对象类型 [英] Chrome Console and Javascript Object Type

查看:294
本文介绍了Chrome控制台和Javascript对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个Javascipt对象的类型(构造函数的名称),但我一直使用所有我尝试过的方法获得一个通用的Object。我在网上搜索,没有找到我的Javascript方法,但还是有效。它始终只是返回对象构造函数类型作为通用的对象。然而,当我在Chrome控制台中检查它时,它正盯着我。任何想法如何我可以在JS中获取这些信息?



PS。我使用Browserify的要求来分开我的代码,如果这有所作为。




我认为这可能是因为Browserify如何加载 require 代码是因为这是我从Browserify加载的构造函数获得的输出:


以下是我通过手动创建构造函数得到的结果:


无论如何也不管我如何创建构造函数 var Prey = function(){} vs var Prey = function Prey(){} 即使Javascript看起来不知道,Chrome开发工具似乎仍然知道构造函数的名称是什么。也许这是因为他们可以用JavaScript语言无法访问的方式检查虚拟机中的代码,但我想知道我是否缺少某些东西。 有没有人知道用另一种方法确定Javascript中的实例类型?

当您执行函数Prey(){}; 时,看到的是Prey,因此函数名称为Prey。如果将Prey定义为 var Prey = function(){}; ,则函数名称为;

所以基本上如果你想使用函数名,你必须给你的构造函数一个名字。 var Prey = function Prey(){};



编辑:或者您可能做了这种变化:

  var A =函数NAMED(){}; 
var B = function(){};
B.prototype = Object.create(A.prototype);
var o = new B();

> o
< B {};

> o.constructor.name
< NAMED


I would like to find the type of a Javascipt object (the name of the constructor function) but I keep just getting a generic "Object" back with all methods I have tried. I have searched online and no Javascript method I have found has work for me yet. It always just returns the object constructor type as a generic "Object". Yet there it is, staring right at me when I inspect it in the Chrome console. Any ideas how I might get that information in JS?

PS. I am using Browserify's require to separate my code if that make a difference.

The reason I thought it might be because of how Browserify loads require code is because this is the output I get from a Browserify loaded constructor function:

And here is what I get from creating a constructor function manually:

Regardless of that and regardless of how I create my constructor function var Prey = function() {} vs var Prey = function Prey() {} the Chrome dev tools still seem to know what the name of the constructor function was even when Javascript does not seem to know. Perhaps this is because they can inspect the code in the virtual machine in a way the the Javascript language does not have access to but I am wondering if I am missing something. Does anyone know of another way to determine an instance type in Javascript?

解决方案

The Prey you are seeing is the Prey when you do function Prey(){};, thus, the function name is Prey. If you define Prey as var Prey = function(){}; then the function name is "";

So basically if you want use the function name, you must give your constructor a name. var Prey = function Prey(){};

EDIT: or its possible that you did a variation of this:

var A = function NAMED(){};
var B = function(){};
B.prototype = Object.create(A.prototype);
var o = new B();

> o 
< B {};

> o.constructor.name
< "NAMED"

这篇关于Chrome控制台和Javascript对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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