Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 895 Bytes

Tip_function_call.md

File metadata and controls

40 lines (30 loc) · 895 Bytes

Tips: Function.apply

  • build an array, first item will be the first paramter of the function
  • use Function.call(null, [paramtersArray])

Example:

function existy(x) { return x != null}

function cat() {
	var head = _.first(arguments);
    if (existy(head)) {
    	return [].concat.apply(head, _.rest(arguments));
    } else {
    	return [];
    }
}

function construct(head, tail) {
	return cat([head], _.toArray(tail));
}

function project(table, keys) {
	return _.map(table, function(obj) {
	    // construct(obj, keys): build paramter array
	    // equal to: _.pick(obj, keys);
    	return _.pick.apply(null, construct(obj, keys));
    })
}

var lib = [{title:'SICP', isbn:'1', ed:1},
		   {title:'SICP', isbn:'2', ed:2}];

var result = project(lib, ['title', 'isbn']);

console.log('result:', result)
console.log(_.pick({title:'SICP', isbn:'1', ed:1}, 'title'))