Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Prototype not Working

Hi I don't know whether this is my mistake in understanding Javascript prototype object ..

Well to be clear I'm new to the Javascript singleton concept and lack clear cut knowledge in that but going through some referral sites I made a sample code for my system but it's giving out some errors which I couldn't find why so I'm asking for your help. My code is:

referrelSystem = function(){
//Some code here
}();

Prototype function:

referrelSystem.prototype.postToFb = function(){
//Some Code here
};

I get an error saying prototype is undefined!

Excuse me i thought of this right now

EDIT

I have used like this:

referrelSystem = function(){
 return{
        login:getSignedIn,
        initTwitter:initTw
    }
};

Is this causing an issue?

like image 713
Harish Avatar asked Apr 25 '26 17:04

Harish


1 Answers

A typical way to define a JavaScript class with prototypes would be:

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

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

You might have been confused with the self-executing function syntax (closures). That is used when you would like to have "private" members in your class. Anything you declare in this closure will only be visible within the closure itself:

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;
})();

I would recommend that you study common module patterns if you're looking into creating a library.

like image 154
Ates Goral Avatar answered Apr 27 '26 07:04

Ates Goral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!