JavaScript 原型不工作
嗨,我不知道这是否是我在理解 Javascript 原型对象方面的错误..
需要明确的是,我对 Javascript 单例概念很陌生,并且缺乏明确的知识,但是通过一些推荐网站,我为其制作了示例代码我的系统,但它给出了一些错误,我找不到原因,所以我请求你的帮助。我的代码是:
referrelSystem = function(){
//Some code here
}();
原型函数:
referrelSystem.prototype.postToFb = function(){
//Some Code here
};
我收到一条错误消息,说原型未定义!
对不起,我现在想到了这个
编辑
我已经这样使用了:
referrelSystem = function(){
return{
login:getSignedIn,
initTwitter:initTw
}
};
这会引起问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用原型定义 JavaScript 类的典型方法是:
您可能对自执行函数语法(闭包)感到困惑。当您希望班级中有“私人”成员时,可以使用此选项。您在此闭包中声明的任何内容都仅在闭包本身内可见:
我建议您学习 常见模块模式(如果您正在考虑创建库)。
A typical way to define a JavaScript class with prototypes would be:
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:
I would recommend that you study common module patterns if you're looking into creating a library.
更新:查看更新后的代码,
referrelSystem
的返回
将无法按预期工作,因为调用new 时返回值会被丢弃referrelSystem()。
不要返回对象,而是将这些属性设置为
this
(构造的referrelSystem 实例):Update: Seeing your updated code, the
return
fromreferrelSystem
won't work as expected, since return values are discarded when callingnew referrelSystem()
.Rather than returning an object, set those properties to
this
(the instance of referrelSystem that gets constructed):我不认为你打算立即执行这些函数,请将它们更改为:
(+var, -())
与原型函数相同:(
这里你不需要 var,因为你要分配给已经存在。)
I don't think you intend to immediately execute the functions, change them to this:
(+var, -())
Same with the prototype function:
(Here you don't need the var, because you're assigning to something that already exists.)
函数应该
返回
才能工作属性。
在这里查看这个示例
A function should
return
to work asproperty.
Take a look at this example here