I am trying to create my object through a function, but I am unable to figure out the syntax for the getter function.
var myObject =
{
0:123,
get a()
{
return this[0];
}
}
console.log("This works: " + myObject.a);
function test()
{
this[0] = 123;
// error
this.a = get function()
{
return this[0];
};
}
var myTest = new test();
console.log(myTest.a);
Within the test function, the assignment of the get function throws a missing semicolon error and if I remove the keyword "function", it says that get is not defined.
How can I assign a getter function to the current object within my function?
var f = get function(){...}syntax is correct, usevar f=get {...}instead. Your functiontestfails to parse, while removing thefunction()makes it work