Reputation: 5985
So, I have a large object called con
in that object, I have many variables, numbered sort of like an excel sheet, b19, b20, b21, etc.
I am trying to return a value from each one, but when I do a console log, It logs the entire function, not just the return.
Here's how the object is set up:
var con = {
b13: function(){
return 12600.535*Math.sqrt((con.b14+459.4)/459.4)
},
b14: function(){
return 20;
}
}
$(document).ready(function(){
console.log(con.b13);
});
This outputs this into the console:
function(){
return 12600.535*Math.sqrt((con.b14+459.4)/459.4)
}
So how do I format this so that it outputs the actual number in the equation?
Upvotes: 0
Views: 49
Reputation:
The problem is that your object's properties are functions
, but you are trying to call them as if they were values
.
For example, if you wanted correctly log con.b13
's value to the console, you would need to change the command to:
console.log(con.b13());
What this does is get what con.b13
returns rather than what it is.
If you don't want to go through the hassle of adding a ()
next to every reference, you can modify the object and define getters
like this:
var con = {
get b13() {
return 12600.535 * Math.sqrt((con.b14 + 459.4) / 459.4)
},
get b14() {
return 20;
}
}
If you define the object like this, your original command console.log(con.b13)
will work as intended.
Upvotes: 0
Reputation: 43718
You can simply use es5 getters/setters.
var con = {
get b13() {
return 12600.535*Math.sqrt((con.b14+459.4)/459.4);
},
get b14() {
return 20;
}
};
Upvotes: 0
Reputation: 1814
Try console.log(con.b13());
. You are logging the function definition not executing it.
Upvotes: 2
Reputation: 169018
You need to make b13
and b14
properties with a getter function:
var con = {};
Object.defineProperty(con, "b13", {
get: function() {
return 12600.535*Math.sqrt((con.b14+459.4)/459.4);
}
});
Object.defineProperty(con, "b14", {
get: function() { return 20; }
});
This will cause con.b13
and con.b14
to call the given functions, returning whatever the functions return.
Upvotes: 2
Reputation: 452
you don't define the properties as functions...
var con = {
b13: 239487,
b12: 923748
};
edit: if some properties need to be functions you have to call them e.g. con.b14(), not con.b14 as a property
Upvotes: 1