this.String = {
Get : function (val) {
return function() {
return val;
}
}
};
What is the ':' doing?
什么是':'在做什么?
4 个解决方案
#1
Others have already explained what this code does. It creates an object (called this.String
) that contains a single function (called Get
). I'd like to explain when you could use this function.
其他人已经解释了这段代码的作用。它创建一个包含单个函数(称为Get)的对象(称为this.String)。我想解释你什么时候可以使用这个功能。
This function can be useful in cases where you need a higher order function (that is a function that expects another function as its argument).
在需要更高阶函数的情况下(此函数需要另一个函数作为其参数),此函数非常有用。
Say you have a function that does something to each element of an Array
, lets call it map
. You could use this function like so:
假设您有一个函数可以对Array的每个元素执行某些操作,我们可以将其称为map。你可以像这样使用这个函数:
function inc (x)
{
return x + 1;
}
var arr = [1, 2, 3];
var newArr = arr.map(inc);
What the map
function will do, is create a new array containing the values [2, 3, 4]
. It will do this by calling the function inc
with each element of the array.
map函数将执行的操作是创建一个包含值[2,3,4]的新数组。它将通过使用数组的每个元素调用函数inc来完成此操作。
Now, if you use this method a lot, you might continuously be calling map
with all sorts of arguments:
现在,如果你经常使用这个方法,你可能会不断地用各种参数调用map:
arr.map(inc); // to increase each element
arr.map(even); // to create a list of booleans (even or odd)
arr.map(toString); // to create a list of strings
If for some reason you'd want to replace the entire array with the same string (but keeping the array of the same size), you could call it like so:
如果由于某种原因你想要用相同的字符串替换整个数组(但保持相同大小的数组),你可以像这样调用它:
arr.map(this.String.Get("my String"));
This will create a new array of the same size as arr
, but just containing the string "my String"
over and over again.
这将创建一个与arr大小相同的新数组,但只是一遍又一遍地包含字符串“my String”。
Note that in some languages, this function is predefined and called const
or constant
(since it will always return the same value, each time you call it, no matter what its arguments are).
请注意,在某些语言中,此函数是预定义的并且称为const或constant(因为无论它的参数是什么,每次调用它时它总是返回相同的值)。
Now, if you think that this example isn't very useful, I would agree with you. But there are cases, when programming with higher order functions, when this technique is used.
现在,如果你认为这个例子不是很有用,我会同意你的看法。但是有些情况下,当使用更高阶函数进行编程时,使用这种技术时。
For example, it can be useful if you have a tree you want to 'clear' of its values but keep the structure of the tree. You could do tree.map(this.String.Get("default value"))
and get a whole new tree is created that has the exact same shape as the original, but none of its values.
例如,如果您想要“清除”其值但保留树的结构,则可能很有用。你可以做tree.map(this.String.Get(“默认值”))并获得一个全新的树,它具有与原始完全相同的形状,但没有任何值。
#2
this.String = {}
specifies an object. Get
is a property of that object. In javascript, object properties and their values are separated by a colon ':'.
this.String = {}指定一个对象。 Get是该对象的属性。在javascript中,对象属性及其值由冒号':'分隔。
So, per the example, you would call the function like this
因此,根据示例,您可以像这样调用函数
this.String.Get('some string');
More examples:
var foo = {
bar : 'foobar',
other : {
a : 'wowza'
}
}
alert(foo.bar); //alerts 'foobar'
alert(foo.other.a) //alerts 'wowza'
#3
It assigns an object that has a property "Get" to this.String
. "Get" is assigned an anonymous function, which will return a function that just returns the argument that was given to the first returning function. Sounds strange, but here is how it can be used:
它为this.String分配一个具有属性“Get”的对象。 “Get”被赋予一个匿名函数,该函数将返回一个函数,该函数只返回给第一个返回函数的参数。听起来很奇怪,但这是如何使用它:
var ten = this.String["Get"](10)();
ten
will then contain a 10. Instead, you could have written the equivalent
然后,十个将包含10.相反,你可以写出等价物
var ten = this.String.Get(10)();
// saving the returned function can have more use:
var generatingFunction = this.String.Get("something");
alert(generatingFunction()); // displays "something"
That is, :
just assigns some value to a property.
也就是说:只是为一个属性赋予一些价值。
#4
This answer may be a bit superflous since Tom's is a good answer but just to boil it down and be complete:-
这个答案可能有点夸张,因为汤姆是一个很好的答案,但只是把它煮熟并完成: -
this.String = {};
Adds an object to the current object with the property name of String
.
使用属性名称String将对象添加到当前对象。
var fn = function(val) {
return function() { return(val); }
}
Returns a function from a closure which in turn returns the parameter used in creating the closure. Hence:-
从闭包返回一个函数,该函数又返回用于创建闭包的参数。因此: -
var fnInner = fn("Hello World!");
alert(fnInner()); // Displays Hello World!
In combination then:-
然后组合: -
this.String = { Get: function(val) {
return function() { return(val); }
}
Adds an object to the current object with the property name of String
that has a method called Get
that returns a function from a closure which in turn returns the parameter used in creating the closure.
将对象添加到当前对象,其属性名称为String,其具有名为Get的方法,该方法从闭包返回一个函数,该闭包又返回用于创建闭包的参数。
var fnInner = this.String.Get("Yasso!");
alert(fnInner()); //displays Yasso!
#1
Others have already explained what this code does. It creates an object (called this.String
) that contains a single function (called Get
). I'd like to explain when you could use this function.
其他人已经解释了这段代码的作用。它创建一个包含单个函数(称为Get)的对象(称为this.String)。我想解释你什么时候可以使用这个功能。
This function can be useful in cases where you need a higher order function (that is a function that expects another function as its argument).
在需要更高阶函数的情况下(此函数需要另一个函数作为其参数),此函数非常有用。
Say you have a function that does something to each element of an Array
, lets call it map
. You could use this function like so:
假设您有一个函数可以对Array的每个元素执行某些操作,我们可以将其称为map。你可以像这样使用这个函数:
function inc (x)
{
return x + 1;
}
var arr = [1, 2, 3];
var newArr = arr.map(inc);
What the map
function will do, is create a new array containing the values [2, 3, 4]
. It will do this by calling the function inc
with each element of the array.
map函数将执行的操作是创建一个包含值[2,3,4]的新数组。它将通过使用数组的每个元素调用函数inc来完成此操作。
Now, if you use this method a lot, you might continuously be calling map
with all sorts of arguments:
现在,如果你经常使用这个方法,你可能会不断地用各种参数调用map:
arr.map(inc); // to increase each element
arr.map(even); // to create a list of booleans (even or odd)
arr.map(toString); // to create a list of strings
If for some reason you'd want to replace the entire array with the same string (but keeping the array of the same size), you could call it like so:
如果由于某种原因你想要用相同的字符串替换整个数组(但保持相同大小的数组),你可以像这样调用它:
arr.map(this.String.Get("my String"));
This will create a new array of the same size as arr
, but just containing the string "my String"
over and over again.
这将创建一个与arr大小相同的新数组,但只是一遍又一遍地包含字符串“my String”。
Note that in some languages, this function is predefined and called const
or constant
(since it will always return the same value, each time you call it, no matter what its arguments are).
请注意,在某些语言中,此函数是预定义的并且称为const或constant(因为无论它的参数是什么,每次调用它时它总是返回相同的值)。
Now, if you think that this example isn't very useful, I would agree with you. But there are cases, when programming with higher order functions, when this technique is used.
现在,如果你认为这个例子不是很有用,我会同意你的看法。但是有些情况下,当使用更高阶函数进行编程时,使用这种技术时。
For example, it can be useful if you have a tree you want to 'clear' of its values but keep the structure of the tree. You could do tree.map(this.String.Get("default value"))
and get a whole new tree is created that has the exact same shape as the original, but none of its values.
例如,如果您想要“清除”其值但保留树的结构,则可能很有用。你可以做tree.map(this.String.Get(“默认值”))并获得一个全新的树,它具有与原始完全相同的形状,但没有任何值。
#2
this.String = {}
specifies an object. Get
is a property of that object. In javascript, object properties and their values are separated by a colon ':'.
this.String = {}指定一个对象。 Get是该对象的属性。在javascript中,对象属性及其值由冒号':'分隔。
So, per the example, you would call the function like this
因此,根据示例,您可以像这样调用函数
this.String.Get('some string');
More examples:
var foo = {
bar : 'foobar',
other : {
a : 'wowza'
}
}
alert(foo.bar); //alerts 'foobar'
alert(foo.other.a) //alerts 'wowza'
#3
It assigns an object that has a property "Get" to this.String
. "Get" is assigned an anonymous function, which will return a function that just returns the argument that was given to the first returning function. Sounds strange, but here is how it can be used:
它为this.String分配一个具有属性“Get”的对象。 “Get”被赋予一个匿名函数,该函数将返回一个函数,该函数只返回给第一个返回函数的参数。听起来很奇怪,但这是如何使用它:
var ten = this.String["Get"](10)();
ten
will then contain a 10. Instead, you could have written the equivalent
然后,十个将包含10.相反,你可以写出等价物
var ten = this.String.Get(10)();
// saving the returned function can have more use:
var generatingFunction = this.String.Get("something");
alert(generatingFunction()); // displays "something"
That is, :
just assigns some value to a property.
也就是说:只是为一个属性赋予一些价值。
#4
This answer may be a bit superflous since Tom's is a good answer but just to boil it down and be complete:-
这个答案可能有点夸张,因为汤姆是一个很好的答案,但只是把它煮熟并完成: -
this.String = {};
Adds an object to the current object with the property name of String
.
使用属性名称String将对象添加到当前对象。
var fn = function(val) {
return function() { return(val); }
}
Returns a function from a closure which in turn returns the parameter used in creating the closure. Hence:-
从闭包返回一个函数,该函数又返回用于创建闭包的参数。因此: -
var fnInner = fn("Hello World!");
alert(fnInner()); // Displays Hello World!
In combination then:-
然后组合: -
this.String = { Get: function(val) {
return function() { return(val); }
}
Adds an object to the current object with the property name of String
that has a method called Get
that returns a function from a closure which in turn returns the parameter used in creating the closure.
将对象添加到当前对象,其属性名称为String,其具有名为Get的方法,该方法从闭包返回一个函数,该闭包又返回用于创建闭包的参数。
var fnInner = this.String.Get("Yasso!");
alert(fnInner()); //displays Yasso!