可以将对象的属性传递给函数,而不需要定义参数,并通过对象的键来使用它们
Is possible to pass to a function the properties of an object without the arguments defined and use them by the object's keys?
快速而奇怪的问题:
我有一个对象(在本例中很小,但在项目中较大):
var myObject = {
hello: 1, // easier I think
'hey.ya': 5 // quite impossible but the first option is valid too
}
那么我想以某种方式传递给一个函数并使用"hello"例如在闭包中,像这样
function x(){
// my closure
return function(){this.init = function(){alert(hello)}, this.heyYa = function(){alert(/* I do not know how to call the other hey.ya variable */)}}
}
var myClass = x(), instance = new myClass(); instance.init();
谢谢!
您需要使用myObject
var myObject = {
hello: 1,
'hey.ya': 5
}
function x(obj){
return function(){
this.init = function(){
alert(obj.hello)
},
this.heyYa = function(){
alert(obj['hey.ya'])
}
}
}
var myClass = x(myObject);
var instance = new myClass();
instance.init(); // alerts '1'
instance.heyYa(); // alerts '5'
相关文章:
- JS for循环来设置HTML对象参数
- 如何在使用javascript加载页面时直接设置对象参数
- 如何将对象参数传递给事件处理程序
- 在ng重复中剪切对象参数的字符串长度
- 在新对象参数中使用“this”选择器
- 在 javascript 中通过引用传递一个对象参数
- 为什么 Javascript 中的参数在使用 console.log 时返回 [对象参数]
- 如何使用 javascript 访问在函数中传递的对象参数
- 在哪里可以获得有关 JavaScript 函数的对象参数语法的信息
- JavaScript 对象参数在通过网络读取时更改
- 我正在寻找一种智能方法来检查传递给函数的对象参数
- 使用来自另一个函数的对象参数调用函数
- Javascript中的对象参数,以及我们应该如何命名它们
- Jquery 在 Firefox 中的自定义对象参数(动态导航加载动态内容)
- 有效记忆对象参数
- AngularJS v1.2.x $digest() 无限循环指令隔离范围对象参数
- 在 Jasmine 中执行有关对象参数的断言
- Jquery - 编辑对象参数
- 检查对象参数是否具有所有必需属性的最有效方法是什么
- Javascript对象参数的分配和使用