Javascript 原型不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4953825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Prototype not Working
提问by Harish
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:
需要明确的是,我是 Javascript 单例概念的新手,在这方面缺乏明确的知识,但是通过一些推荐网站,我为我的系统制作了一个示例代码,但它给出了一些我无法找到原因的错误,所以我我请求你的帮助。我的代码是:
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?
这会导致问题吗?
采纳答案by David Tang
Update: Seeing your updated code, the returnfrom referrelSystemwon't work as expected, since return values are discarded when calling new referrelSystem().
更新:看到更新后的代码,returnfromreferrelSystem不会按预期工作,因为调用new referrelSystem().
Rather than returning an object, set those properties to this(the instance of referrelSystem that gets constructed):
不是返回一个对象,而是将这些属性设置为this(构造的 referrelSystem 实例):
var referrelSystem = function () {
// I assume you have other code here
this.login = getSignedIn;
this.initTwitter = initTw;
};
回答by Ates Goral
A typical way to define a JavaScript class with prototypes would be:
使用原型定义 JavaScript 类的典型方法是:
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 patternsif you're looking into creating a library.
如果您正在考虑创建一个库,我建议您研究常见的模块模式。
回答by david
I don't think you intend to immediately execute the functions, change them to this:
我认为您不打算立即执行这些功能,请将它们更改为:
var referrelSystem = function(){
//Some code here
};
(+var, -())
(+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.)
(在这里您不需要 var,因为您要分配给已经存在的东西。)

