JavaScript中的静态变量

本文翻译自:Static variables in JavaScript

如何在Javascript中创建静态变量?


#1楼

参考:https://stackoom.com/question/6RUF/JavaScript中的静态变量


#2楼

The closest thing in JavaScript to a static variable is a global variable - this is simply a variable declared outside the scope of a function or object literal: JavaScript中与静态变量最接近的是全局变量 - 这只是在函数或对象文字范围之外声明的变量:

var thisIsGlobal = 1;

function foo() {
    var thisIsNot = 2;
}

The other thing you could do would be to store global variables inside an object literal like this: 您可以做的另一件事是将全局变量存储在对象文字中,如下所示:

var foo = { bar : 1 }

And then access the variabels like this: foo.bar . 然后像这样访问变量: foo.bar


#3楼

窗口级变量就像静态一样,你可以使用直接引用,这些变量可用于应用的所有部分


#4楼

You might take advantage of the fact that JS functions are also objects -- which means they can have properties. 您可以利用JS函数也是对象的事实 - 这意味着它们可以具有属性。

For instance, quoting the example given on the (now vanished) article Static variables in Javascript : 例如,引用Javascript中的(现已消失的)文章静态变量给出的示例:

function countMyself() {
    // Check to see if the counter has been initialized
    if ( typeof countMyself.counter == 'undefined' ) {
        // It has not... perform the initialization
        countMyself.counter = 0;
    }

    // Do something stupid to indicate the value
    alert(++countMyself.counter);
}

If you call that function several time, you'll see the counter is being incremented. 如果您多次调用该函数,您将看到计数器正在递增。

And this is probably a much better solution than poluting the global namespace with a global variable. 这可能是一个比使用全局变量来规划全局命名空间更好的解决方案。


And here is another possible solution, based on a closure : Trick to use static variables in javascript : 这是另一个可能的解决方案,基于一个闭包: 在javascript中使用静态变量的技巧

var uniqueID = (function() {
   var id = 0; // This is the private persistent value
   // The outer function returns a nested function that has access
   // to the persistent value.  It is this nested function we're storing
   // in the variable uniqueID above.
   return function() { return id++; };  // Return and increment
})(); // Invoke the outer function after defining it.

Which gets you the same kind of result -- except, this time, the incremented value is returned, instead of displayed. 这会得到相同类型的结果 - 除此之外,返回递增的值而不是显示。


#5楼

There is no such thing as an static variable in Javascript. 在Javascript中没有静态变量这样的东西。 This language is prototype-based object orientated, so there are no classes, but prototypes from where objects "copy" themselves. 这种语言是基于原型的面向对象的,因此没有类,而是来自对象“复制”自身的原型。

You may simulate them with global variables or with prototyping (adding a property to the prototype): 您可以使用全局变量或原型来模拟它们(向原型添加属性):

function circle(){
}
circle.prototype.pi=3.14159

#6楼

If you come from a class-based, statically typed object-oriented language (like Java, C++ or C#) I assume that you are trying to create a variable or method associated to a "type" but not to an instance. 如果你来自基于类的,静态类型的面向对象语言(如Java,C ++或C#),我假设您正在尝试创建与“类型”相关但不与实例相关联的变量或方法。

An example using a "classical" approach, with constructor functions maybe could help you to catch the concepts of basic OO JavaScript: 使用“经典”方法的示例,使用构造函数可能可以帮助您捕获基本OO JavaScript的概念:

function MyClass () { // constructor function
  var privateVariable = "foo";  // Private variable 

  this.publicVariable = "bar";  // Public variable 

  this.privilegedMethod = function () {  // Public Method
    alert(privateVariable);
  };
}

// Instance method will be available to all instances but only load once in memory 
MyClass.prototype.publicMethod = function () {    
  alert(this.publicVariable);
};

// Static variable shared by all instances
MyClass.staticProperty = "baz";

var myInstance = new MyClass();

staticProperty is defined in the MyClass object (which is a function) and has nothing to do with its created instances, JavaScript treats functions as first-class objects , so being an object, you can assign properties to a function. staticProperty在MyClass对象(它是一个函数)中定义,与其创建的实例无关,JavaScript将函数视为第一类对象 ,因此作为对象,您可以为函数指定属性。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值