javascript静态的表现class继承非静态class
Performance of javascript static class inherit non static class
我已经构建了一个 javascript class 可以对 dom 执行操作。为了尽可能简化从外部的使用,我现在有这样的方法:
hello.add(id);
hello.delete(id);
hello.select(id);
为了完成上述工作,我设置了一个使用非静态 class 的静态 class,如下所示:
静态class
class hello {
static add(id) {
let hello = new HelloCore();
hello.add(id);
}
static delete(id) {
let hello = new HelloCore();
hello.delete(id);
}
static select(id) {
let hello = new HelloCore();
hello.select(id);
}
}
非静态class
真正的代码是 500 行,带有构造函数等等。
class helloCore() {
add() {
// Do something
}
delete() {
// Do something
}
select() {
// Do something
}
}
现在非常简单,但我想知道这种方法是否存在性能问题。我的猜测是它在每次静态调用内存时存储一个新的 javascript 对象?
- 我需要担心吗?
- 如果是,有什么更好的方法,但仍然超级简单?
我想远离的
从一开始我就一直需要将对象传递给将要使用它的函数。见下文。
let hello = new HelloCore();
function my_function(hello) {
let id = 2;
hello.add(id);
}
my_function(hello);
使用静态方法,我不需要到处传递对象。我可以从任何地方触发该功能。
我可以使用全局 var hello = new Hello();
但我需要确保首先加载它,我不会忘记它,这对用户来说意味着额外的代码。
您可能想看看 Singleton。
它完全符合您想要实现的目标。
但是,我必须承认我(和许多其他开发人员)不喜欢单例、全局变量和静态变量 类,不是因为性能,而是作为一种通用模式。
This SO question 讲述了为什么单例不好。
@mareful 评论了这个有用的信息:
With the static class approach the hello instance is not available after function call any more...
表示每次调用后静态class会自动清理
然后我收到了@Bergi 的评论,他提出使用 const
包含具有相同结果的函数的想法。这种方法非常有效。
之前 - 使用静态 class
class hello {
static add(id) {
let hello = new HelloCore();
hello.add(id);
}
static delete(id) {
let hello = new HelloCore();
hello.delete(id);
}
static select(id) {
let hello = new HelloCore();
hello.select(id);
}
}
之后 - 使用 const
const hello = {
add(id) {
new HelloCore().add(id);
},
delete(id) {
new HelloCore().delete(id);
},
select(id) {
new HelloCore().select(id);
}
};
在这两种情况下,我都可以像 hello.add(id);
那样称呼它。 const
版本更短,在这种情况下也有一些其他优点。
it doesn't create a constructor function and prototype object that are never needed (and might even be abused, or at least will confuse the reader).
我已经构建了一个 javascript class 可以对 dom 执行操作。为了尽可能简化从外部的使用,我现在有这样的方法:
hello.add(id);
hello.delete(id);
hello.select(id);
为了完成上述工作,我设置了一个使用非静态 class 的静态 class,如下所示:
静态class
class hello {
static add(id) {
let hello = new HelloCore();
hello.add(id);
}
static delete(id) {
let hello = new HelloCore();
hello.delete(id);
}
static select(id) {
let hello = new HelloCore();
hello.select(id);
}
}
非静态class
真正的代码是 500 行,带有构造函数等等。
class helloCore() {
add() {
// Do something
}
delete() {
// Do something
}
select() {
// Do something
}
}
现在非常简单,但我想知道这种方法是否存在性能问题。我的猜测是它在每次静态调用内存时存储一个新的 javascript 对象?
- 我需要担心吗?
- 如果是,有什么更好的方法,但仍然超级简单?
我想远离的
从一开始我就一直需要将对象传递给将要使用它的函数。见下文。
let hello = new HelloCore();
function my_function(hello) {
let id = 2;
hello.add(id);
}
my_function(hello);
使用静态方法,我不需要到处传递对象。我可以从任何地方触发该功能。
我可以使用全局 var hello = new Hello();
但我需要确保首先加载它,我不会忘记它,这对用户来说意味着额外的代码。
您可能想看看 Singleton。
它完全符合您想要实现的目标。
但是,我必须承认我(和许多其他开发人员)不喜欢单例、全局变量和静态变量 类,不是因为性能,而是作为一种通用模式。
This SO question 讲述了为什么单例不好。
@mareful 评论了这个有用的信息:
With the static class approach the hello instance is not available after function call any more...
表示每次调用后静态class会自动清理
然后我收到了@Bergi 的评论,他提出使用 const
包含具有相同结果的函数的想法。这种方法非常有效。
之前 - 使用静态 class
class hello {
static add(id) {
let hello = new HelloCore();
hello.add(id);
}
static delete(id) {
let hello = new HelloCore();
hello.delete(id);
}
static select(id) {
let hello = new HelloCore();
hello.select(id);
}
}
之后 - 使用 const
const hello = {
add(id) {
new HelloCore().add(id);
},
delete(id) {
new HelloCore().delete(id);
},
select(id) {
new HelloCore().select(id);
}
};
在这两种情况下,我都可以像 hello.add(id);
那样称呼它。 const
版本更短,在这种情况下也有一些其他优点。
it doesn't create a constructor function and prototype object that are never needed (and might even be abused, or at least will confuse the reader).