2014-03-03 142 views
0

我聲明瞭一個對象「會員」在我的應用程序的全局變量:JavaScript函數調用

var MyShulApp = MyShulApp || {}; 

MyShulApp.Members = (function() { 

    function Members() { 

    } 

    Members.prototype.getMembersList = function() { 

     var list = [{ name: "john", age: 90 }, { name: "kaly", age: 90 }, { name: "steve", age: 90}]; 

     return list; 
    } 

    return Members; 
}) 

我希望能夠調用getMembersList(),所以我這樣做:

var lst = oMembers.getMembersList(); 

但它不工作,我得到undefined。

這工作:

var membersList = oMembers.prototype.getMembersList(); 

我在做什麼錯?

編輯(照片添加): enter image description here

+1

如何定義「oMembers」? – maerics

+1

你需要撥打你的匿名功能 – Grundy

+0

@Grundy:發佈那個答案! – maerics

回答

1

我不知道什麼是oMembers

但我的猜測是,你想使用一個自動執行功能:

var MyShulApp = MyShulApp || {}; 
MyShulApp.Members = (function() { 
    function Members() {} 
    Members.prototype.getMembersList = function() { 
     var list = [{ name: "john", age: 90 }, { name: "kaly", age: 90 }, { name: "steve", age: 90}]; 
     return list; 
    } 
    return Members; 
})(); 

注意我在最後添加(),執行功能。

但你並不需要的是,你可以只使用

var MyShulApp = MyShulApp || {}; 
MyShulApp.Members = function Members() {}; 
MyShulApp.Members.prototype.getMembersList = function() { 
    var list = [{ name: "john", age: 90 }, { name: "kaly", age: 90 }, { name: "steve", age: 90}]; 
    return list; 
};