How to determine if an object is an object literal

2019-03-09 20:51发布

Is there any way to determine in Javascript if an object was created using object-literal notation or using a constructor method?

It seems to me that you just access it's parent object, but if the object you are passing in doesn't have a reference to it's parent, I don't think you can tell this, can you?

7条回答
倾城 Initia
2楼-- · 2019-03-09 21:27

There is no way to tell the difference between an object built from an object literal, and one built from other means.

It's a bit like asking if you can determine whether a numeric variable was constructed by assigning the value '2' or '3-1';

If you need to do this, you'd have to put some specific signature into your object literal to detect later.

查看更多
登录 后发表回答