Javascript原型不工作

Har*_*ish 2 javascript prototype object

嗨,我不知道这是否是我理解Javascript原型对象的错误..

很清楚我是Javascript单身概念的新手并且缺乏清晰的知识,但是通过一些推荐网站我为我的系统制作了一个示例代码,但它发出了一些我无法找到的错误,所以我'我在寻求你的帮助.我的代码是:

referrelSystem = function(){
//Some code here
}();
Run Code Online (Sandbox Code Playgroud)

原型功能:

referrelSystem.prototype.postToFb = function(){
//Some Code here
};
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,说原型未定义!

对不起,我现在想到了这个

编辑

我用过这样的:

referrelSystem = function(){
 return{
        login:getSignedIn,
        initTwitter:initTw
    }
};
Run Code Online (Sandbox Code Playgroud)

这会导致问题吗?

Ate*_*ral 5

使用原型定义JavaScript类的典型方法是:

function ReferrelSystem() {
    // this is your constructor
    // use this.foo = bar to assign properties
}

ReferrelSystem.prototype.postToFb = function () {
    // this is a class method
};
Run Code Online (Sandbox Code Playgroud)

您可能会对自执行函数语法(闭包)感到困惑.当您希望在班级中拥有"私人"成员时使用.您在此闭包中声明的任何内容仅在闭包本身中可见:

var ReferrelSystem = (function () {
    function doSomething() {
        // this is a "private" function
        // make sure you call it with doSomething.call(this)
        // to be able to access class members
    }

    var cnt; // this is a "private" property

    function RS() {
        // this is your constructor
    }

    RS.prototype.postToFb = function () {
        // this is a class method
    };

    return RS;
})();
Run Code Online (Sandbox Code Playgroud)

如果您正在研究创建库,我建议您学习常见的模块模式.