我有這樣的代碼:爲什麼`this`永遠是構造函數中的`Window`對象?
(function() {
function App(elements, options) {
if (!this instanceof App) return new App(elements, options);
var that = this;
return this;
}
window.App = App;
})();
App(document.querySelectorAll('.Slider-slide'), {
interval: 5000
});
我的問題是,它從不創建的App
一個新的實例,因此,this
進一步下跌的代碼始終是Window
對象,任何想法,爲什麼?
因爲你不與沒有新的呼叫 – megawac
@megawac intializing應用必要:http://ejohn.org/blog/simple-class-instantiation/ – benhowdle89
我提供了一個答案的例子,將使案件真正@ benhowdle89 – megawac