How to access private method from another private method in Javascript
I was wondering if it was possible to call a private method from another private method on Javascript. I have some code like the following:
function Balloon() {
function density( altitude, gas ) {
/* KG/CU M */
var gas = {
/* GAS DEFINATIONS - wolframalpha.com */
"hydrogen" : .00100794,
"helium" : .004002602,
"nitrogen" : .0140067,
"methane" : .0160425,
"ammonia" : .0170305,
"neon" : .0201791,
"dry air" : .0289644
}
var alt = {
/* CONSTANTS - http://en.wikipedia.org/wiki/Density_of_air#Altitude */
"p0" : 101325, // Sea level standard atmospheric pressure (Pa)
"T0" : 288.15, // Sea level standard temperature (K)
"g" : 9.80665, // Earth-surface gravitational acceleration (m/s^2)
"L" : 0.0065, // Temperature lapse rate (K/m)
"R" : 8.31447 // Universal gas constant (mol * K)
}
var temperature = alt["T0"] - alt["L"] * altitude;
var pressure = alt["p0"] * (1 - ((开发者_运维技巧 alt["L"] * altitude ) / alt["T0"] )) ^ (( alt["g"] * gas[gas] ) / ( alt["R"] * alt["L"] ));
var density = ( pressure * gas[gas] ) / ( alt["R"] * temperature );
return density;
}
function lift( altitude, gas ) {
/* KG/CU M */
return density( altitude, "dry air" ) - density( altitude, gas );
}
this.requiredGas = function( altitude, gas, ratio, weight ) {
return (( weight / 1000 ) * ratio ) / lift( altitude, gas );
}
}
and am trying to access it like:
balloon = new Balloon();
var required = balloon.requiredGas(10, "helium", 1.5, 4530);
I have seen people declare this
from outside the private functions like so, but don't know if that's how to approach this one.
You are overriding the argument you pass into your density
function with the gas
array.
See, it works fine once I rename it!
http://jsfiddle.net/pEcMJ/
You are redefining your method parameter function density( altitude, gas )
with an object var gas ={}
This line produces the NAN
var pressure = alt["p0"] * (1 - (( alt["L"] * altitude ) / alt["T0"] )) ^ (( alt["g"] * gas[gas] ) / ( alt["R"] * alt["L"] ));
It is gas[gas]
:)
精彩评论