I’m teachimg myself Javascript by porting a C# algebra expression compiler to Javascript. I have an OpBank object that stores the attributes of each operator, e.g., it’s symbol and a pointer to a function that implements the operator. I know JS doesn’t have function pointers but I don’t know what else to call it.
When I try to load the page, I get the following error:
“Microsoft JScript runtime error: Unable to get value of the property ‘Plus’: object is null or undefined”
The error occurs when the browser tries to parse the Operators array at the bottom, it’s complaining about the OpBank.Plus. I tried OpBank.Plus, OpBank.Plus(), just plain old Plus, and this.Plus. In any case I can’t get it to recognize the Plus function when the Operators array gets loaded.
var OpBank = {
Plus: function (left, right) {
return (left + right);
},
Minus: function (left, right) {
return (left - right);
},
Operators: [
(new KeyValuePair("+", new ABinaryOperator("+", 1, OpType.Binary, "+", OpBank.Plus))),
(new KeyValuePair("-", new ABinaryOperator("-", 1, OpType.Binary, "-", OpBank.Minus)))
]
}
Is what I’m attempting not possible? Should I use the constructor syntax for OpBank instead, i.e., var ObBank = function(){} then use prototype to add the functions and array? I didn’t want to do that because I need only one OpBank for the life of the app and I don’t want every method to have to instantiate a new OpBank because it’s a pretty big object (only part of it is shown above).
OpBankdoes not exist yet while the object literal is evaluated.You can simply split it into two steps though: