如何跟踪 JavaScript 方法中的先前值?

发布于 2024-11-05 05:33:06 字数 238 浏览 0 评论 0原文

我需要将方法中的当前整数与先前的整数进行比较。看起来这样的事情应该有效,但事实并非如此。有人能告诉我问题出在哪里吗?请注意,电流是在方法外部设置的。

myMethod : function() {
    var previous;

    if ( current > previous ) {
        // do this!
    }

    previous = current;
}

I need to compare a current integer with a previous integar within a method. It seems like something like this should work, but it doesn't. Can someone tell me where the problem lies? Note current is set outside the method.

myMethod : function() {
    var previous;

    if ( current > previous ) {
        // do this!
    }

    previous = current;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-11-12 05:33:10

似乎您正在尝试实现记忆功能。有一个关于如何进行操作的很好的教程 这里

Seems like you are trying to implement a memoization function. There is a good tutorial on how to go about it here.

迷你仙 2024-11-12 05:33:09

每次调用 myMethod 时,都会重新声明 previous (var previous)。

您有四种可能性:

(A)创建一个闭包(在我看来是最好的解决方案,但取决于您的需求):(

myMethod : (function() {
    var previous = null;
    return function() {
        if ( current > previous ) {
            // do this!
        }  
        previous = current;
    }
}());

B)将 previous 设置为函数对象的属性:

myMethod : function() {
    if ( current > foo.myMethod.previous ) {
        // do this!
    }   
    foo.myMethod.previous = current;
}

foo.myMethod.previous = null;

但这将函数与对象的命名。

(C) 如果它适合您的模型,则将 previous 设为对象的属性 myMethod 是以下属性的属性:

previous: null,
myMethod : function() {
    if ( current > this.previous ) {
        // do this!
    }
    this.previous = current;
}

(D) 与 (A) 类似,设置 previous 更高范围之外的某个地方:在我看来,

var previous = null;
// ...
myMethod : function() {

    if ( current > previous ) {
        // do this!
    }  
    previous = current;
}

这不是一个好主意,因为它污染了更高范围。

如果没有看到更多代码,很难判断,但是当您将 current 传递给函数时,情况可能会更好。

Every time you call myMethod, previous is declared anew (var previous).

You have four possibilities:

(A) Create a closure (best solution imo, but depends on your needs):

myMethod : (function() {
    var previous = null;
    return function() {
        if ( current > previous ) {
            // do this!
        }  
        previous = current;
    }
}());

(B) Set previous as property of the function object:

myMethod : function() {
    if ( current > foo.myMethod.previous ) {
        // do this!
    }   
    foo.myMethod.previous = current;
}

foo.myMethod.previous = null;

But this ties the function very much to the naming of the object.

(C) If it fits in your model, make previous a property of the object myMethod is a property of:

previous: null,
myMethod : function() {
    if ( current > this.previous ) {
        // do this!
    }
    this.previous = current;
}

(D) Similar to (A), set previous somewhere outside in a higher scope:

var previous = null;
// ...
myMethod : function() {

    if ( current > previous ) {
        // do this!
    }  
    previous = current;
}

This is not a good imo as it pollutes the higher scope.

Without seeing more of your code it is hard to tell, but it is probably also better when you pass current to the function.

安穩 2024-11-12 05:33:09

你只需要保持状态。

var previousState = 'something';

function myMethod(current)
{
    if(current > previousState)
       // Do Something

    previousState = current;
}

You just need to maintain state.

var previousState = 'something';

function myMethod(current)
{
    if(current > previousState)
       // Do Something

    previousState = current;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文