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
4
Question by Fattie · Jun 02, 2012 at 11:47 AM · javascriptreference

Your "own" structs ... are they indeed passed by value?

I'd like to check something with a Unityscript expert .... thanks!

Generally in Javascript - as I understand it - if something is a struct it is passed by value. (A new copy is made - if you change it you are doing nothing to the original). Whereas, if something is a class, it's passed by reference. (No copy is made of the data. You are actually changing the values in the original.) [See footnote 1]

My question .. as an everyday matter in UnityScript, you make "structs" or pseudo-structs, like this ...

 class EnormousThing extends System.ValueType
 {
 // huge amounts of stuff here
 }


In fact, is it correct that such "pseudo-structs" are ALSO passed by value?

Another way to express this:

If you use "class EnormousThing" it will be passed by reference ("just as with all classes in Javascript"), whereas, if you use the "pseudo-struct" idiom: "class EnormousThing extends System.ValueType" it will be passed by value (|just as with real structs in Javascript")

is this exactly correct?


{As a further bonus question, in fact, is the only difference in adding "extends System.ValueType" in fact the value-versus-reference behaviour so described? Is that the only reason to add "extends System.ValueType" ..? Is there really any other difference in using that qualifier?}


[Footnote 1] Although - this is a detail - cleverly the reference itself is I believe just a copy, so, if you set it to null you have done nothing to the original. Please completely ignore this issue in the question at hand.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by IGoByChad · Jun 02, 2012 at 02:18 PM

It is correct that it will be passed by value. Another difference is that you will no longer be able to use a default constructor to initialize your values. You will get the default on all your members. (This link might help: http://answers.unity3d.com/questions/23044/struct-in-javascript.html

Comment
Add comment · Show 1 · 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 Fattie · Jun 02, 2012 at 02:24 PM 0
Share

"It is correct that it will be passed by value."

Fantastic. Thanks for the confirmation!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityScript - Class reference 1 Answer

Raycast script help? 1 Answer

Namespace declaration for UnityScript or Unity Javascript 0 Answers

#define directive in UnityScript 1 Answer

Is it possible to pass a variable by reference in javascript 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