我想知道我使用的模式是否有名称(是模式吗?)。例如,不要像这样使用:
var MyFakeClass = function(propertie) {
this.propertie = propertie
this.init();
};
MyFakeClass.prototype.method = function() {
// something here
};
var instanceOfFakeClass = new MyFakeClass('propertie');
instanceOfFakeClass.method();
我这样做:
var MyFakeClass = {
init: function(propertie) {
this.propertie = propertie;
this.method();
},
method: function() {
// something here
}
};
MyFakeClass.init('propertie');
所以,init方法调用该方法,我不需要从外部调用。
谢谢。
最佳答案
有很多不同的方式来描述它,并且可能没有一个始终用于此目的的绰号:
- 对象字面量
- 静态声明的对象
- 静态声明的单例
“单例”在这里可能是一个有用的词,因为它描述了一个仅打算成为其中之一的对象。有许多不同的可能方法来声明单例。您的声明就是这样一种方式。
“静态声明”通过使用 new
创建的构造函数将第二个选项与第一个选项区分开来。
关于javascript - 方法调用对象内部的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33205157/