[英]javascript - convert function into variable
I googled a bit and it seems I either don't know what I'm asking or this isn't possible. 我在Google上搜索了一下,似乎我不知道我在问什么,或者这是不可能的。 I think it's probably the former. 我认为可能是前者。
If I have an object 如果我有一个对象
obj = {
func : function(){
return 'hello';
}
}
and I normally do alert(obj.func())
I of course get "hello"
我通常会alert(obj.func())
我当然会收到"hello"
But if I wanted to have obj.func
be simply a variable, is that possible somehow? 但是,如果我想让obj.func
只是一个变量,那有可能吗? I won't ever pass parameters to the function, I just want to reference its return value as a plain variable like alert(obj.func)
, not a function call. 我永远不会将参数传递给函数,我只想将其返回值作为普通变量(例如alert(obj.func)
,而不是函数调用。
Thanks. 谢谢。
Try this : 尝试这个 :
obj = {
func : (function(){
return 'hello';
})()
}
Yes, thats easy! 是的,那很简单!
var obj = {
func : 'hello'
};
alert(obj.func);
see this working fiddle 看到这个工作的小提琴
if you want it to be a calculated value or sth. 如果您希望它是一个计算值或某物。 you can still let it be a function but callable like a simple property: 您仍然可以让它成为一个函数,但是可以像一个简单的属性一样调用:
var obj = {
func : (function(){return 'hello';})()
};
What you are talking about is a getter : it is a function that works as a property. 您所说的是一个吸气剂:它是一个可作为属性工作的函数。 It is standard since javascript 1.8.5, which means older browser won't handle it. 自javascript 1.8.5起它是标准的,这意味着较旧的浏览器将无法处理它。
The syntax is as follow, using your example : 语法如下,使用您的示例:
obj = {
get func() { return "hello" ; }
};
// use like this :
console.log(obj.func);
Most likely you will want to define a setter also : this is also a function behaving like a property. 您很可能还希望定义一个setter:这也是一个像属性一样的函数。 The syntax will be : 语法为:
obj = {
get func() { return this._innerText ; },
set func(txt) { this._innerText = txt ; },
_innerText : "hello"
};
console.log(obj.func) ; // output is hello
obj.func = "godd bye" ;
console.log(obj.func) ; // output is good bye
Obviously, as often with simple example, this one is of no big use: 显然,与通常的示例一样,该示例没有太大用处:
Let us see a degree to radian converter using a getter / setter : 让我们看一下使用getter / setter转换弧度的度数:
var Angle = {
get degree () { return this._degree ; },
set degree (dg) { this._degree = dg ; },
get radian () { return 2*Math.PI*this.degree/360 ; },
set radian (rd) { this._degree = rd * 360 / (2*Math.PI) ; },
_degree : 0
} ;
// you can use :
Angle.radian = Math.PI / 2;
console.log ( Angle.degree ); // -->> output is 90
Angle.degree=45 ;
console.log(Angle.radian) ; // --> output is 0.7853... which is PI/4
You might also have a look on Object.defineProperty, which allows to define both standard properties, and getters/setters, but with more option, especially the option to 'hide' a property, which allows it not to be enumerated. 您可能还会看到Object.defineProperty,它可以定义标准属性和getters / setter,但还有更多选择,尤其是“隐藏”属性的选项,该属性不允许枚举。
For more information : 欲获得更多信息 :
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/get https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/set https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Operators/get https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/set https:// developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/defineProperty
var varFunc = function() { alert("hello"); }
varFunc();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.