I have the following Javascript:
function TestModel()
{
this.name = "Testvalue";
this.id = 0;
}
var test = new TestModel();
alert(test.name);
If I want to initialize an object with different parameters without the use of a constructor I would need to call the following code:
var test = new TestModel();
test.name = "other value";
test.id = 5;
alert(test.name);
What I would like to do is something similar to the C# style:
class TestModel
{
public string name = "Testvalue";
public int id = 0;
}
TestModel test = new TestModel() { name = "other value", id = 5 };
System.Windows.MessageBox.Show(test.name);
But if I try something like that in javascript it won’t work.
In short: I am trying to init a object in 1 line without the use of a constructor. Is this possible in javascript?
This looks most like the C# syntax: