How can I get value or component type by string?
I want to judge a variable in code, such as ‘cc.SpriteFrame’, ‘cc.Animation’, ‘cc.AnimationClip’ or ‘cc.Prefab’.
Thanks.
-Hiroki
How can I get value or component type by string?
I want to judge a variable in code, such as ‘cc.SpriteFrame’, ‘cc.Animation’, ‘cc.AnimationClip’ or ‘cc.Prefab’.
Thanks.
-Hiroki
Does anyone have any advice?
It seems Cocos-Creator editor distinguishing type of files, since they are not accepting non coresponding type files.
I want to do same.

Thanks.
-Hiroki
Take a look at “Property declaration” part:
http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
Your own components can also be used as property type:
cc.Class({
properties : {
myComp : {
type : require('MyComponentScript'),
default : null,
}
}
Hi persy,
Thank you for the answer but what I want to do is judge a variable and / or component it my code.
#My question might not clear. sorry.
Your suggested document seems not explaining about how to judge.
Thanks.
-Hiroki
You can use JavaScript operators typeof and instanceof to check type of a value.
If you want to have a variable that can only have values of a certain type - I believe there is no support for this in JavaScript, you may want to check TypeScript
Thanks.
I realized what I wanted to do with the code below.
return_type = function (x) {
if (typeof (x) == 'object') {
if (x instanceof cc.SpriteFrame) {
return 'SpriteFrame';
}
if (x instanceof cc.AnimationClip) {
return 'AnimationClip';
}
if (x instanceof cc.Prefab) {
return 'Prefab';
}
if (x instanceof cc.Component) {
return 'Component';
}
// .....
return 'Unknown object';
}
return typeof(x);
};