使用我现有的函数计算新值而不替换现有值

Use my existing function to calculate new values without replacing existing ones

我有这个计算潮汐的代码。函数很大,用日期和端口来计算。

有 4 个结果:

haute1

haute2

basse1

basse2 

现在,我想计算 4 个新值(对于日期 +1)而不用新日期触发 all 函数,因为它会改变 haute1haute2basse1basse2.. 我只想使用现有函数创建新值“haute1_tomorrow”。

知道是否可行吗?

我正在做的是像这样调用现有函数:

maree("city, tomorrow_date);

tomorrow_date 为明天的日期。但它只会触发函数并为 haute1haute2basse1basse2 设置新值。 我想要的是保留这些值,并创建 4 个新值 haute1_tomorrowhaute2_tomorrowbasse1_tomorrowbasse2_tomorrow

我的函数(我不会粘贴整个函数,因为它太长了,您无法阅读):

function get haute1():String { return $pmm; };
function get haute2():String { return $pms; }
function get basse1():String { return $bmm; };
function get basse2():String { return $bms; };

// convenient way to retrieve public data

    function get results():Object
    {
        if (!$pmm) return {}; 

        return {
            haute1:$pmm,
            haute2:$pms,
            basse1:$bmm,
            basse2:$bms,
        };
    }

    function maree($inputport:String = "", $inputdate:String = "", onDataCompleteFunc:Function = null):void 
    {
        if (onDataCompleteFunc != null) _onDataCompleteFunc = onDataCompleteFunc;

        $port_maree = trim($inputport).toUpperCase();   

        /*if ($inputdate != '') $infodate = getdate(date2timestamp($inputdate + " 00:00:00","d/m/Y h:i:s"));
        else $infodate = getdate(date2timestamp(date("dmY") + " 00:00:00","d/m/Y h:i:s"));

        $jour_maree = $infodate["mday"];
        $mois_maree = $infodate["mon"];
        $annee_maree = $infodate["year"];*/

        var $infodate:Array = $inputdate.split("/");

        $jour_maree = Number($infodate[0]);     // 25;      // Day
        $mois_maree = Number($infodate[1]);     // 12;      // Month
        $annee_maree = Number($infodate[2]);    // 2015;    // Year

        loadXmlData();
    }       

... .. .

你需要OOP面向对象编程

不管您的实际问题如何,解决此类问题的基本方法是制作一个 component/class 可以自给自足地解决您的问题的一个实例。例如:

package
{
    public class Trigonometry
    {
        public var degrees:Number;
        public var radians:Number;

        public var sine:Number;
        public var cosine:Number;
        public var tangent:Number;

        public function Trigonometry(value:Number)
        {
            // Normalize degrees to -180..+180.
            value %= 360;

            if (value >= 180) value -= 360;
            if (value < -180) value += 360;

            degrees = value;
            radians = value * Math.PI / 2;

            sine    = Math.sin(radians);
            cosine  = Math.cos(radians);
            tangent = Math.tan(radians);
        }
    }
}

然后你使用实例,它们是独立和隔离的,不会混淆彼此的字段:

var A30:Trigonometry = new Trigonometry(30);
var A45:Trigonometry = new Trigonometry(45);
var A60:Trigonometry = new Trigonometry(60);
var A90:Trigonometry = new Trigonometry(90);

trace("Sine of 30 degrees is:", A30.sine);
trace("Cosine of 60 degrees is:", A60.cosine);
trace("Tangent of 45 degrees is:", A45.tangent);

UPD: 那么对于你的任务,你需要这样的东西:

package
{
    public class Mar
    {
        // Here you need to declare all the variables you are going to use.
        // Like (I don't know what type you need):
        public var $pmm:String;
        public var $pms:String;

        private var callback:Function;

        // Then put all the methods you need:
        public function maree($inputport:String = "", $inputdate:String = "", handler:Function = null):void
        {
            callback = handler;

            // Your processing here.
        }
    }
}

那你用起来一模一样:

var M1:Mar = new Mar;
var M2:Mar = new Mar;

M1.maree(/* first call arguments here */);
M2.maree(/* second call arguments here */);

因此实例将在不考虑彼此存在的情况下运行,并且您可以访问它们各自的字段,例如:

trace(M1.$pmm);
trace(M1.$pms);