I am trying to call an object method from an object (the same object) property definition to no avail.
var objectName = {
method : function() {
return "boop";
},
property : this.method()
};
In this example I want to assign the return value of objectName.method (“boop”) to objectName.property.
I have tried objectName.method(), method(), window.objectName.method(), along with the bracket notation variants of all those as well, ex. this["method"], with no luck.
At initialization
thisdoes not refer to the object holding the propertymethod(which is not yet initialized) but to the curent context – and since this has nomethodproperty you will get a TypeError.If it is a custom
getteryou want, then you might look into using getters and setters in javascript – they are not supported by ECMAscript prior to ES5, but many engines support them nonetheless.