最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

object - Javascript Prototype not Working - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Feb 10, 2011 at 15:29 clockworkgeek 37.7k9 gold badges91 silver badges127 bronze badges asked Feb 10, 2011 at 5:48 HarishHarish 2,3244 gold badges24 silver badges28 bronze badges 1
  • What are you returning in your first assignment to referrelSystem? Unless you return a Function, it won't have a prototype property. – Ateş Göral Commented Feb 10, 2011 at 5:51
Add a ment  | 

4 Answers 4

Reset to default 5

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 remend that you study mon module patterns if you're looking into creating a library.

Update: Seeing your updated code, the return from referrelSystem won't work as expected, since return values are discarded when calling new referrelSystem().

Rather than returning an object, set those properties to this (the instance of referrelSystem that gets constructed):

var referrelSystem = function () {
    // I assume you have other code here

    this.login = getSignedIn;
    this.initTwitter = initTw;
};

I don't think you intend to immediately execute the functions, change them to this:

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

(+var, -())

Same with the prototype function:

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

(Here you don't need the var, because you're assigning to something that already exists.)

A function should return to work as

prototype

property. Take a look at this example here

发布评论

评论列表(0)

  1. 暂无评论