Red*_*dro 3 javascript constructor object instanceof instance
这个问题确实必须相当简单,但我不能google或duckduck答案,可能是因为99%的初学者教程涉及Person使用name属性创建一个对象(与constructor.name无关但污染了搜索结果).
的实例Object对象可以使用instance.constructor.name来检测:
var foo = new Object;
foo.constructor.name; // "Object"
Run Code Online (Sandbox Code Playgroud)
如何在我自己的对象上设置构造函数名称?
var Bar = function(){};
var baz = new Bar;
baz.constructor.name // "" <- Empty string
Run Code Online (Sandbox Code Playgroud)
nnn*_*nnn 10
Bar您可以执行以下操作,而不是将构造函数定义为设置为匿名函数的变量:
function Bar() {}
Run Code Online (Sandbox Code Playgroud)
或这个:
var Bar = function ConstructorNameHere() {};
Run Code Online (Sandbox Code Playgroud)
凡ConstructorNameHere名字,其实是可以Bar再次:
var Bar = function Bar() {};
Run Code Online (Sandbox Code Playgroud)
(这应该适用于Chrome和FF,对IE不确定但不太有希望.)
| 归档时间: |
|
| 查看次数: |
2642 次 |
| 最近记录: |