Javascript局部静态变量 [英] Javascript local static variable

查看:64
本文介绍了Javascript局部静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我是否完全理解我在这里找到的类似问题的答案,因此请尝试绝对确定:

我想在一个函数中有一个局部变量,仅初始化一次(类似于C,C ++等强类型语言中的静态变量).

当然,我可以在全局范围内声明它,但是将其包含在该函数的范围内似乎是一种更好的做法,因为在其他任何地方都没有使用它.

现在,这是我的工作:

 函数func(data){func.PARAMS = [{"name":"from","size":160,"indexed":true},{"name":"input","size":256,"indexed":false},{"name":输出","size":256,"indexed":false},];...} 

我的问题是, func.PARAMS 确实仅被初始化一次,还是在每次调用该函数时都被初始化?

根据我找到的一些答案(例如,),我需要在初始化之前添加类似以下内容:

  if(typeof func.PARAMS =='未定义') 

这种补充"在强类型语言中当然是无关紧要的,所以我只想确保绝对必要以确保静态行为"(即一次性初始化).

解决方案

除了使用函数对象的属性(如您在示例中所做的那样)外,还有3种其他方法可以在Javascript中模拟函数局部静态变量./p>

所有这些都依赖于闭包,但是使用不同的语法.

方法1(在旧浏览器中受支持):

  var someFunc1 =(function(){var staticVar = 0;返回函数(){alert(++ staticVar);}})();someFunc1();//打印1someFunc1();//打印2someFunc1();//打印3 

方法2(在旧的浏览器中也支持):

  var someFunc2;与({staticVar:0})var someFunc2 = function(){alert(++ staticVar);};someFunc2();//打印1someFunc2();//打印2someFunc2();//打印3 

方法3(需要对EcmaScript 2015的支持):

  {令staticVar = 0;函数someFunc3(){alert(++ staticVar);}}someFunc3();//打印1someFunc3();//打印2someFunc3();//打印3 

用于严格模式的方法3 :

 严格使用"{令staticVar = 0;var someFunc3 = function(){alert(++ staticVar);};}someFunc3();//打印1someFunc3();//打印2someFunc3();//打印3 

Not sure I completely understand answers to similar questions that I found here, so trying to be absolutely sure:

I would like to have a local variable in a function, initialized only once (similar to static variables in strongly-typed languages such as C, C++, etc).

Of course, I could declare it globally, but it seems better practice to have it within the scope of that function, since it is not used anywhere else.

Now, here is what I do:

function func(data) {
    func.PARAMS = [
        {"name": "from", "size": 160, "indexed": true},
        {"name": "input", "size": 256, "indexed": false},
        {"name": "output", "size": 256, "indexed": false},
    ];
    ...
}

And my question is, will func.PARAMS indeed be initialized only once, or will it be initialized every time the function is called?

According to some of the answers that I found (this one for example), I need to precede the initialization with something like:

if (typeof func.PARAMS == 'undefined')

This "supplemental" would be irrelevant in strongly-typed languages of course, so I just want to be sure that it is absolutely necessary in order to ensure "static behavior" (i.e., one-time initialization).

解决方案

In addition to using properties of the function object, as you do in your example, there are 3 other ways to emulate function-local static variables in Javascript.

All of them rely on a closure, but using different syntax.

Method 1 (supported in old browsers):

var someFunc1 = (function(){
    var staticVar = 0 ;
    return function(){
        alert(++staticVar) ;
    }
})() ;

someFunc1() ; //prints 1
someFunc1() ; //prints 2
someFunc1() ; //prints 3

Method 2 (also supported in old browsers):

var someFunc2 ;
with({staticVar:0})
    var someFunc2 = function(){
        alert(++staticVar) ;
    } ;

someFunc2() ; //prints 1
someFunc2() ; //prints 2
someFunc2() ; //prints 3

Method 3 (requires support for EcmaScript 2015):

{
    let staticVar = 0 ;
    function someFunc3(){
        alert(++staticVar) ;
    }
}

someFunc3() ; //prints 1
someFunc3() ; //prints 2
someFunc3() ; //prints 3

Method 3 for strict mode:

'use strict'
{
    let staticVar = 0 ;
    var someFunc3 = function(){
        alert(++staticVar) ;
    } ;
}

someFunc3() ; //prints 1
someFunc3() ; //prints 2
someFunc3() ; //prints 3

这篇关于Javascript局部静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆