I found this page here about calling a function from a string. At the bottom there's an update that states it's better to use window[functionName](params)
, and gives this example:
我在这里找到了关于从字符串调用函数的页面。在底部有一个更新,声明最好使用window [functionName](params),并给出了这个例子:
var strFun = "someFunction";
var strParam = "this is the parameter";
//Create the function
var fn = window[strFun];
//Call the function
fn(strParam);
So, I wrote this:
所以,我写了这个:
var OnChange = function( e )
{
var oContainer = _.DOM.GetElementByAttribute( document, 'data-instance', _testInstance );
var evt = ( e ) ? e : window.event;
var objects = GetObjects( oContainer );
var files = [];
if( evt.target.files ) // html5 .. multiple?
{
for( var i = 0; i < evt.target.files.length; i++ )
files.push( evt.target.files[i].name ); // + " (" + Format.Size( evt.target.files[i].size ) + ")" );
}
else
{
files.push( evt.target.value );
}
var strFunc = "User.UploadPicture";
var test = window[strFunc];
test(e); // error
};
And I'm getting the following error:
而且我收到以下错误:
Uncaught TypeError: test is not a function
未捕获的TypeError:test不是函数
User.Upload
is a function inside the js file. Am I missing something?
User.Upload是js文件中的一个函数。我错过了什么吗?
1 个解决方案
#1
4
Assuming that User
is a globally scoped object containing a function UploadPicture
, you would need to call it like this:
假设User是一个包含函数UploadPicture的全局范围对象,您需要像这样调用它:
var test = window["User"]["UploadPicture"];
test(e);
window["User.UploadPicture"]
refers to a property called, literally, "User.UploadPicture" on the window
object, and is nothing to do with the User
object.
window [“User.UploadPicture”]是指在窗口对象上称为“User.UploadPicture”的属性,与User对象无关。
In this instance however, the page you've linked to seems to be talking about calling a function that you'll determine the name of at runtime. If, in your case, you're always going to call User.UploadPicture
, there's nothing wrong with calling it directly:
但是,在这种情况下,您链接到的页面似乎在讨论调用一个函数,您将在运行时确定其名称。在你的情况下,如果你总是要调用User.UploadPicture,那么直接调用它是没有错的:
User.UploadPicture(e);
You only need to use the other methods if you don't know the name when you're writing the code, and instead want to dynamically invoke different functions at runtime.
如果在编写代码时不知道名称,则只需要使用其他方法,而是希望在运行时动态调用不同的函数。
#1
4
Assuming that User
is a globally scoped object containing a function UploadPicture
, you would need to call it like this:
假设User是一个包含函数UploadPicture的全局范围对象,您需要像这样调用它:
var test = window["User"]["UploadPicture"];
test(e);
window["User.UploadPicture"]
refers to a property called, literally, "User.UploadPicture" on the window
object, and is nothing to do with the User
object.
window [“User.UploadPicture”]是指在窗口对象上称为“User.UploadPicture”的属性,与User对象无关。
In this instance however, the page you've linked to seems to be talking about calling a function that you'll determine the name of at runtime. If, in your case, you're always going to call User.UploadPicture
, there's nothing wrong with calling it directly:
但是,在这种情况下,您链接到的页面似乎在讨论调用一个函数,您将在运行时确定其名称。在你的情况下,如果你总是要调用User.UploadPicture,那么直接调用它是没有错的:
User.UploadPicture(e);
You only need to use the other methods if you don't know the name when you're writing the code, and instead want to dynamically invoke different functions at runtime.
如果在编写代码时不知道名称,则只需要使用其他方法,而是希望在运行时动态调用不同的函数。