我想问一下我的以下OOP风格的优点。 我以下面的方式编写我的JS类。这是做JS OOP的好方法吗?
var MyClass = function() {
// private vars
var self = this,
_foo = 1,
_bar = "test";
// public vars
this.cool = true;
// private methods
var initialize = function(a, b) {
// initialize everything
};
var doSomething = function() {
var test = 34;
_foo = cool;
};
// public methods
this.startRequest = function() {
};
// call the constructor
initialize.apply(this, arguments);
};
var instance_1 = new MyClass();
var instance_2 = new MyClass("just", "testing");
这是一个好方法吗?有什么缺点吗? 我不使用继承,但它会以这种方式实现继承吗?
在此先感谢。
+1 - 最重要的方面是封装和多态性。 – Everyone 2009-08-13 09:09:04
也在这里。在面向对象的许多文章中,继承肯定是过分的重量。在过去的几年中,我刚刚开始有成效地开展OO编程,并且我已经失去了因过度依赖继承而导致的新手错误的数量。 – 2011-07-11 01:57:45