简体   繁体   中英

What is this javascript code doing?

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

What is the ':' doing?

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'

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM