charlie_says Posted July 19, 2018 Share Posted July 19, 2018 I'm trying to write a function that's can have several different elements passed through to it. It could receive a texture, or a sprite, or a container. I'd then be able to handle what happens next... But, as all those items return typeof Object, I'm a little uncertain as to how best to differentiate. Is there a way I can test for they type? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted July 19, 2018 Share Posted July 19, 2018 Fields. Sprite has vertexData, Mesh has vertices, Texture has frame. charlie_says 1 Quote Link to comment Share on other sites More sharing options...
charlie_says Posted July 19, 2018 Author Share Posted July 19, 2018 Thanks @ivan.popelyshev My colleague laughed at me for asking this... He said, why not try instanceof. I've given it a test, and it seems to work fine. Is there any reason not to do this? ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted July 19, 2018 Share Posted July 19, 2018 instanceof works, but in general case, JS doesn't have types, its ambigious, so its fine use fields to make custom "instanceof". For example, in TS you can make functions like `isSprite(d: DisplayObject ) : d is Sprite { return d.vertexData !== undefined }`. Its very big topic, I cant just explain it in one line, I just say that your question makes sense. charlie_says 1 Quote Link to comment Share on other sites More sharing options...
themoonrat Posted July 19, 2018 Share Posted July 19, 2018 If you wanted to go down that route, you can also do const prettyImage = new PIXI.Sprite(); const constructorName = prettyImage.constructor.name; // will return 'Sprite' https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name charlie_says 1 Quote Link to comment Share on other sites More sharing options...
jonforum Posted July 19, 2018 Share Posted July 19, 2018 on my side i use .name pixi guys say they don't use so it safe to use and name all your objets. also , more read https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Function/name https://dmitripavlutin.com/understanding-constructor-property/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.