Properties with getters/setters

In C#, it is possible to define special functions that can be accessed as if they were variables. For instance, we could say foo.someVar = "testing";, and under the hood, there are get and set functions, which process the argument testing and store it internally. However, they could also do any other processing on it, for instance, capitalizing the first letter before storing it. So you're not just doing a variable assignment, you're calling a function that sets the variable, and it can do whatever the functions do.

C#:

public class MyClass {     
private int foo = 8;  //"backing store"      
public int Foo {         
    get {             
       return foo;         
    }         
    set {             
      foo = value;             
    }     
} 
}

However, in Unity JavaScript, we can also use get and set functions similar to the C# version, but we need to write the class body whenever you want to use the get or set function.

JavaScript:

public class MyClass {     
private var foo = 8;  //"backing store"      
function get Foo () : int {
  return foo;
} 
function set Foo (value) {
  foo = value;
}      
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.226.255