简体   繁体   English

如何使用静态方法从类中访问变量?

[英]How to access variables from class using static methods?

//my class
function myClass()
{
   this.pubVar = 'hello public';
   var priVar = 'hello private';


}

myClass.staticMethod = function()
{

   //how do i access thos variables here

}

I tried alert(this.pubVar) and alert(priVar) with no success. 我尝试过alert(this.pubVar)alert(priVar)没有成功。 What am I doing wrong? 我究竟做错了什么?

I'm going to change the question a bit.. I didn't explain it well the first time through. 我将稍微改变这个问题。.我第一次没有很好地解释它。

How do I declare static variables in a class? 如何在类中声明静态变量?

I know with java it's simple, you just put static infront of the variable type and presto you've got a static variable. 我知道使用Java很简单,您只需将static放在变量类型的前面,然后就可以拥有一个static变量。 I can't seem to find that option for javascript.. 我似乎找不到用于javascript的选项。

Since you want to use the instance of the object inside of your method, I think that you are not really wanting to create a static method , what you want, is a method that is applicable and accessible by all the instances. 由于您想在方法内部使用该对象的实例,因此我认为您并不是真正想要创建一个静态方法 ,而您想要的是一种适用于所有实例且可以访问的方法。

For doing that you can extend the constructor's prototype : 为此,您可以扩展构造函数的原型

function MyClass() {
   this.pubVar = 'hello public';
   var priVar = 'hello private';
}

MyClass.prototype.myMethod = function() {
   alert(this.pubVar);
}


var instance = new MyClass();
instance.myMethod(); // alerts 'hello public'

When you extend the prototype of a constructor function, all the members that reside on it, will be inherited to the object instances created with the new operator. 扩展构造函数的原型时,驻留在其上的所有成员将继承到使用new运算符创建的对象实例。

So you define this method only once in your constructor function, and it will be accessible in the context of each instance (the this keyword inside the method refers to the object instance). 因此,您只需在构造函数中定义一次此方法,就可以在每个实例的上下文中访问该方法(方法内的this关键字引用对象实例)。

Definition: 定义:

myClass.staticMethod = function(obj)
{
    alert(obj.pubVar);
}

Use: 使用:

var instance = new myClass();
myClass.staticMethod(instance);

You won't be able to use "priVar", since it isn't a variable on the class. 您将无法使用“ priVar”,因为它不是类上的变量。 It's a method-scoped variable. 这是方法范围的变量。

Also, if you need to access properties of the class, why are you making it a static method? 另外,如果您需要访问该类的属性,为什么要使其成为静态方法?

Sorry for the answer on a old post, may be this will help someone 抱歉,旧帖子的答案可能会对您有所帮助

If you want to access a variable from a static method, you can declare that variable as a static property. 如果要从静态方法访问变量,则可以将该变量声明为静态属性。

//my class
function myClass()
{
   this.pubVar = 'hello public';
   var priVar = 'hello private';
}

myClass.staticProperty = 'someValue';

myClass.staticMethod = function()
{
    //This is how you access it
    alert(myClass.staticProperty);
}

Hmm... If you are trying to access "this" then you don't really want a static method. 嗯...如果您尝试访问“ this”,那么您真的不需要静态方法。 A static method belongs to the class and doesn't have access to any given instance of that class, and therefore "this" does not exist in the context of a static method. 静态方法属于该类,并且不能访问该类的任何给定实例,因此,“ this”在静态方法的上下文中不存在。

If you are looking for the code for an instance method it would be something along these lines: 如果您正在寻找实例方法的代码,则可能与以下几行类似:

function myClass()
{
    this.pubVar = 'hello public';
    var priVar = 'hello private';

    this.method = function(){
        alert(priVar);
    }
}

Alternatively, you could pass a reference to an object into your static method, which would look something like this: 或者,您可以将对对象的引用传递到您的静态方法中,如下所示:

function myClass()
{
    this.pubVar = 'hello public';
    var priVar = 'hello private';
}
myClass.staticMethod = function(obj)
{
    alert(obj.pubVar);
}

Hope that helps 希望能有所帮助

It is really simple. 真的很简单。 Of cause you need an instance of class for this purpose: 当然,您需要为此目的一个类的实例:

//my class
function myClass()
{
   this.pubVar = 'hello public';
   var priVar = 'hello private';
}

myClass.staticMethod = function()
{
   //how do i access thos variables here
   alert(this.pubVar);
}

var instance=new myClass(); // this is instance of class
myClass.staticMethod.call(instance)

The only question do you really need to use static then? 唯一的问题是您真的需要使用static吗? Also there are no means to access priVar. 此外,也无法访问priVar。 Because it uses closure. 因为它使用闭包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM