[英]How to call two instances of same non-global JavaScript function with parameters
我试图从HTML代码中调用JavaScript函数的两个不同实例,但是我对变量范围,对象实例化的确切细节,将函数返回给浏览器的必要性,在何处以及如何提供函数的参数感到困惑。 ..我尝试了SO中不同代码段的组合,但无济于事。 我对OOP很熟悉,但对它的功能/ JavaScript风格却不了解。
我的HTML我有类似的东西:
<script type="text/javascript" src="JavaScript.js"></script>
<script type="text/javascript">
var localInstance = new globalFunction();
localInstance.localFunction("someString", "someOtherString");
var otherLocalInstance = new globalFunction();
otherLocalInstance.localFunction("someString2", "someOtherString2");
</script>
在一个名为“ JavaScript.js”的JavaScript文件中,我有类似以下内容:
function globalFunction() {
var string;
var otherstring;
function localFunction(str, ostr) {
string = str;
otherstring = ostr;
// do something more
}
};
我收到了Uncaught TypeError: localInstance.myFunction is not a function
错误。 我究竟做错了什么?
我认为您需要返回函数,以便可以从外部访问它,如下所示:
function globalFunction() {
var string;
var otherstring;
function localFunction(str, ostr) {
string = str;
otherstring = ostr;
// do something more
}
return {
localFunction: localFunction
}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.