Does this way of defining JS objects have any purpose?

I'm maintaining some legacy code and I've noticed that the following pattern for defining objects is used: var MyObject = {}; (function (root) { root.myFunction = function (foo) { //do something }; })(MyObject); Is there any purpose to this? Is it equivalent to just doing the following? var MyObject = { myFunction : function (foo) { //do something }

这种定义JS对象的方式有什么用处吗?

我维护一些遗留代码,我注意到使用了以下用于定义对象的模式: var MyObject = {}; (function (root) { root.myFunction = function (foo) { //do something }; })(MyObject); 这有什么目的吗? 这相当于只是做了以下事情吗? var MyObject = { myFunction : function (foo) { //do something }; }; 我不打算进行一个神圣的探索来重构整个代码库以符合我的喜好,但我真的很想了解这种

JQuery check if variable is function

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers In your example, to get a reference to the testFunction if it is in scope, you can use eval . Yes, I said it, eval , so you have to know that the string could be manipulated by the user to run malicious code if you don't sanitize it for function names; var testContr

JQuery检查变量是否是函数

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 在你的例子中,为了得到testFunction的引用,如果它在范围内,你可以使用eval 。 是的,我说过, eval ,所以你必须知道,如果你没有为函数名称清理它,那么用户可能会操纵该字符串来运行恶意代码; var testController = function() { alert('123'); } $(function() { $('[mfour]').each(function(e, t) { var a = eval($

How to get 'typeof' of the value of a variable

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers Supposing that your function is global you can verify if it exists using window object (the object where are stored the global variables). if (typeof window[functionName] === 'function') { // function exists } JSFIDDLE You can use window[functionName] as follow

如何获得变量值的'typeof'

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 假设你的函数是全局的,你可以使用window对象(存储全局变量的对象)验证它是否存在。 if (typeof window[functionName] === 'function') { // function exists } 的jsfiddle 你可以使用window[functionName] ,如下所示: function Result(){} var functionName = 'Result'; if (typeof window[functionName] == 'function'){

Executing a dynamically named function in Javascript

Possible Duplicate: How to execute a JavaScript function when I have its name as a string Struggling with this one, and I can't seem to find a good resource on it. Background: I'm creating a step system, and I'm passing the direcition/order within an attribute data-step="1" . This controls the ID that will be shown (that parts easy), but also the function that needs t

在Javascript中执行一个动态命名的函数

可能重复: 当我将其名称作为字符串时如何执行JavaScript函数 挣扎着这个,我似乎无法找到一个好的资源。 背景:我正在创建一个步骤系统,并且将属性data-step="1"的方向/顺序传递给了我。 这将控制将显示的ID(该部分很容易),还需要调用以获取正确信息的函数。 问题基本上是 ,我怎样才能调用我需要动态构建的名称的函数? IE: step1(); step2(); step1(); step2(); 除了我想在那里动态地添加那个数字

How to call a function whose name is defined in a string?

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers if A is defined globally, then window["A"]() . However, there's no need to do that in javascript. Just pass the function itself rather than its name: function foo() {...} // BAD function callFunc(someName) { window[someName]() } callFunc("foo") // GOOD

如何调用名称在字符串中定义的函数?

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 如果A是全局定义的,那么window["A"]() 。 但是,没有必要在javascript中这样做。 只需传递函数本身而不是名称: function foo() {...} // BAD function callFunc(someName) { window[someName]() } callFunc("foo") // GOOD function callFunc(someFunc) { someFunc() } callFunc(foo) 喜欢这个: window[varName]()

How do I trigger a function from a string?

Possible Duplicate: How to execute a JavaScript function when I have its name as a string I have this function function myfunc() { alert('it worked!'); } and this variable which is a string var callback = 'myfunc'; How do I trigger the function from that string? Assuming that the function is declared globally it will be contained in the window object so you can call it using window[ca

如何从字符串触发函数?

可能重复: 当我将其名称作为字符串时如何执行JavaScript函数 我有这个功能 function myfunc() { alert('it worked!'); } 和这个变量是一个字符串 var callback = 'myfunc'; 我如何触发该字符串的功能? 假设该函数是全局声明的,它将被包含在window对象中,所以你可以使用window[callback]()来调用它。 全局声明的函数存储在window对象中,因此您可以使用window.myfunc引用myfunc 。 与JavaScript对象的其他属性

Throw Exception containing variable name

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers You are trying to convert a string to a function and create a new instance of a variable, which won't work. There are ways to get a function corresponding to a string: How to turn a String into a javascript function call? But a better way to go about this would be

抛出包含变量名称的异常

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 您正试图将字符串转换为函数并创建一个变量的新实例,这将不起作用。 有几种方法可以获得与字符串相对应的函数:如何将字符串转换为javascript函数调用? 但是更好的方法是简单地执行第一个if语句,或将其转换为开关。 对于全局变量: // Set up in global scope, not in the scope of something else. var NoXException = function

jquery: use string as function (error: string is not a function)

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers How to turn a String into a JavaScript function call? [duplicate] 13 answers It depends a bit on the scope, but the general idea is that you can use the square bracket notation to get a reference to the function and execute it. For example, the following: window[valu

jquery:使用字符串作为函数(错误:字符串不是一个函数)

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 如何将字符串转换为JavaScript函数调用? [复制] 13个回答 它取决于范围,但总体思路是可以使用方括号表示法来获取对函数的引用并执行它。 例如,以下内容: window[value](); 将等同于第一次迭代的callme() ,假定callme函数是全局作用域( window对象的一个​​属性)。 你可以尝试使用eval来评估字符串的功能,尽管你必须小心安

Call a function whose name is stored in a variable

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers To call function use Module[str](); As Module is an object, you can access the dynamic properties and methods of it by using the bracket notation. (function() { var Module = (function() { var fctToCall = function() { console.log('Foo'); }; retu

调用名称存储在变量中的函数

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 调用函数使用 Module[str](); 由于Module是一个对象,因此可以使用括号表示法来访问它的动态属性和方法。 (function() { var Module = (function() { var fctToCall = function() { console.log('Foo'); }; return { fctToCall: fctToCall }; })(); var Module2 = (function() { var i

Call javascript function which name is in variable

This question already has an answer here: How to execute a JavaScript function when I have its name as a string 29 answers window[name]() 您可以通过名称引用来调用函数,方法是将它们选作window的属性并执行它们 have you tried variableName(); ? We often pass around callback functions, which might look something like this function doSomething(callbackFunction){ // some happy code callbac

调用JavaScript函数名称在变量中

这个问题在这里已经有了答案: 当我的名字作为字符串29答案时如何执行JavaScript函数 window[name]() 您可以通过名称引用来调用函数,方法是将它们选作window的属性并执行它们 你有没有尝试variableName(); ? 我们经常传递回调函数,这可能看起来像这样 function doSomething(callbackFunction){ // some happy code callbackFunction() }