Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Essential · Jan 16, 2014 at 01:59 PM · javascriptinstantiateclassproperties

How can I declare class properties of an object in a single line?

Instead of populating a new class variable by declaring its properties one at a time, is it possible to do it in a single line?

I.e., Rather than…

 var myClassInstance : MyClass;
 myClassInstance.name = "a name";
 myClassInstance.place = "a place";
 myClassInstance.number = 32;

…could I do something like the following instead?

 var myClassInstance = new MyClass( "a name", "a place", 32 );
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by poncho · Jan 16, 2014 at 02:27 PM

in c# it is

 MyClass myClassInstance = new MyClass(){name = "a name", place = "a place", number = 32};

not sure about JS but you could try

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jessy · Jan 16, 2014 at 02:14 PM

I use object initializers for Unity objects and structs, because we can't make more constructors for them. I rarely use it for my own classes/structs.

http://msdn.microsoft.com/en-us/library/bb384062.aspx

Please tell us if this works in UnityScript. I can't recommend using UnityScript for any reason, but I'm interested.

Edit: I didn't realize you didn't know about constructors. Learn those first.

http://wiki.unity3d.com/index.php/UnityScript_versus_JavaScript#JavaScript_is_class-free

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image poncho · Jan 16, 2014 at 02:31 PM 0
Share

didnt check'd the links, but this answers pretty much covers the question, upvoted and please select it as the answer

avatar image Essential · Jan 16, 2014 at 03:07 PM 0
Share

Thanks for the links.

So I'm looking into constructors (which you correctly recognized I didn't know about) and they look a useful tool that I'm going to keep learning about. But just so I understand your answer, are you saying that what I want — basically poncho's answer — is not possible to do in UnityScript without using a constructor? Or are you saying that you don't know if it's possible and you're suggesting I should learn how to do it via a constructor?

avatar image poncho · Jan 16, 2014 at 03:42 PM 1
Share

the code I posted is the same as typing
$$anonymous$$yClass myClassInstance = new $$anonymous$$yClass(); myClassInstance.name = "a name"; myClassInstance.place = "a place"; myClassInstance.number = 32;, having the properties or public variables inside the {} its a shortcut for myClassInstance.{propname}, a constructor "myClass()" its the basic class constructor, an overloaded constructor "myClass("a name", "a place", 32)" would need you create a constructor, in the myClass class, a public myClass(string mynamevalue, string myplacevalue, int mynumbervalue) where you only assing the parameter to the value in the object

avatar image Jessy · Jan 16, 2014 at 05:15 PM 0
Share

But like I said, there's really no reason to do this with your own classes. If constructor overloading isn't enough, because you have too many parameters of the same type, then you can use default parameters, and na$$anonymous$$g. var myClassInstance = new $$anonymous$$yClass(name: "a name", place: "a place", number: 32);

avatar image Jessy · Jan 16, 2014 at 05:17 PM 0
Share

@Essential you should learn how to write constructors, and then, if you still want to use UnityScript, find out if you can use object initializers in UnityScript. They can yield very concise, yet descriptive, code.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityScript - Class reference 1 Answer

Are Unity's JS OOP functions customized? 2 Answers

How to correctly convert Object to GameObject 1 Answer

Please help with this code!! 1 Answer

Update call for Multiple GUIText fails 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges