tags:

views:

246

answers:

5
this.String = {
 Get : function (val) {
  return function() {
      return val;
  }
 }
};

What is the ':' doing?

+10  A: 

this.String = {} specifies an object. Get is a property of that object. In javascript, object properties and their values are separated by a colon ':'.

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'
tj111
But what is the point of the example the author has given? I find this question interesting because it's so weird :p
TimothyP
@TimothyP, That wasn't the poster's question though
cletus
I can't believe the number of upvotes on this answer when it doesn't approach an answer to the question apart from describing some very basic object construction code.
AnthonyWJones
@Anthony - agreed, this is an MSDN answer, i.e. half of what you need to know
annakata
A: 

I'm not sure, because I haven't seen a construct like this before, but I'm guessing the first function returns a function that simply returns the parameter passed to the first function. And that is wrapped inside of a getter for this.String, WTF?

Does this even work ? :-) Is there a larger context you can show us?

TimothyP
You get downvoted for trying to figure something out? NiceEven though I was close the the answer and I did say I was guessing, now I know never to answer again unless I'm absolutely sure
TimothyP
+4  A: 

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:

var ten = this.String["Get"](10)();

ten will then contain a 10. Instead, you could have written the equivalent

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.

Johannes Schaub - litb
+1 Long introduction for your last sentence. :-)
Tomalak
ah, nice. i didn't expect my java-script n00biness hit the right words (well, roughly at least) :p i left explaining what exactly ':' does the other ones and concentrated on what that code does at large. thanks mate
Johannes Schaub - litb
+6  A: 

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.

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:

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.

Now, if you use this method a lot, you might continuously be calling map with all sorts of arguments:

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.

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).


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.

Tom Lokhorst
+1. Good example of why we'd want one of these.
AnthonyWJones
+1  A: 

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.

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.

var fnInner = this.String.Get("Yasso!");
alert(fnInner()); //displays Yasso!
AnthonyWJones