gpt4 book ai didi

Javascript原型(prototype)不工作

转载 作者:行者123 更新时间:2023-12-01 15:51:13 26 4
gpt4 key购买 nike

嗨,我不知道这是否是我理解 Javascript 原型(prototype)对象的错误..

很清楚我是 Javascript 单例概念的新手,并且缺乏明确的知识,但是通过一些推荐网站,我为我的系统制作了一个示例代码,但它给出了一些我找不到原因的错误,所以我我寻求你的帮助。我的代码是:

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

原型(prototype)功能:
referrelSystem.prototype.postToFb = function(){
//Some Code here
};

我收到一个错误,说原型(prototype)未定义!

对不起,我现在想到了这个

编辑

我用过这样的:
referrelSystem = function(){
return{
login:getSignedIn,
initTwitter:initTw
}
};

这会引起问题吗?

最佳答案

使用原型(prototype)定义 JavaScript 类的典型方法是:

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

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

您可能对自执行函数语法(闭包)感到困惑。当你想在你的类(class)中有“私有(private)”成员时使用它。您在此闭包中声明的任何内容都只会在闭包本身中可见:
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;
})();

我建议你学习 common module patterns如果您正在研究创建一个库。

关于Javascript原型(prototype)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4953825/

26 4 0
文章推荐: java - XML 到哈希表
文章推荐: java - 谷歌地图V3.3
文章推荐: java - 在Java中解析requestUrl
文章推荐: php - 启用 PDO OCI
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com