我想問一下我的以下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