[英]javascript access class method without initializing object
I have a class foo
, can I access its public method bar
with out initializing fooObj
我有一个
foo
类,是否可以不初始化fooObj
来访问其公共方法bar
var foo = function(){
this.bar = function(){
console.log("I am bar");
}
}
I know I can access bar
like 我知道我可以访问
bar
像
var fooObj = new foo();
fooObj.bar();
In other object oriented language like jave
if I declare bar
static
I can access it with class name like foo.bar
在其他面向对象的语言(如
jave
如果我声明bar
static
,则可以使用类名(如foo.bar
There is no static
concept like Java
but you can do something like 没有像
Java
这样的static
概念,但是您可以做类似的事情
var foo = function(){
// Constructor specific code
}
foo.bar = function(){
console.log("I am bar");
}
This is how singleton will be defined in JavaScript 这就是在JavaScript中定义单例的方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.