开发者

jscript if for global variable

开发者 https://www.devze.com 2023-03-22 07:34 出处:网络
Here\'s my code var isNew = true function limb(a,b) { if (isNew=true) { post(a,b); isNew = false; post (\"first\");

Here's my code

var isNew = true

function limb(a,b)
{
    if (isNew=true) 
    {
        post(a,b);
        isNew = false;
        post ("first");
    }
    else
    {
        post(a,b);
        post ("not first"); 
    }
}
开发者_StackOverflow中文版

The problem I'm having is that else condition is never triggered. I'm assuming value of isNew is never updated, but I have no idea why.


You should use if (isNew===true) to test it.


In some languages = is used both to assign values and to compare them. JavaScript uses different operators for each of these.

x = 10 always means "set x to 10, and give me the value of x".
x == 10 always means "tell me if x is equal to 10".

So your condition could have been if(isNew == true) and it would have worked. You can also just put if(isNew).

0

精彩评论

暂无评论...
验证码 换一张
取 消