问题描述
嗨我不知道这是否是我在理解Javascript原型对象时的错误..
Hi I don't know whether this is my mistake in understanding Javascript prototype object ..
很清楚我是Javascript单例概念的新手,缺乏明确的知识,但通过一些推荐网站,我为我的系统制作了一个示例代码,但它给出了一些我无法找到的错误,所以我要求你的帮助。我的代码是:
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
}();
原型功能:
referrelSystem.prototype.postToFb = function(){
//Some Code here
};
我收到错误说原型未定义!
I get an error saying prototype is undefined!
对不起我现在想到这个
编辑
我使用如下:
referrelSystem = function(){
return{
login:getSignedIn,
initTwitter:initTw
}
};
这会导致问题吗?
推荐答案
更新:查看更新后的代码,返回
从 referrelSystem
将无法按预期工作,因为在调用 new referrelSystem()
时会丢弃返回值。
Update: Seeing your updated code, the return
from referrelSystem
won't work as expected, since return values are discarded when calling new referrelSystem()
.
而不是返回一个对象,将这些属性设置为 this
(构造的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;
};
这篇关于Javascript原型不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!