Javascript 在不同类型的调用中追加的函数
我想创建一个附加参数值的函数。重要的是它应该接受下面给定的函数调用Javascript 在不同类型的调用中追加的函数,javascript,jquery,Javascript,Jquery,我想创建一个附加参数值的函数。重要的是它应该接受下面给定的函数调用 concat('hello', 'world'); concat('hello')('world'); 这两者都应该返回“helloworld”。这是怎么可能的?您可以使用+操作符来连接字符串。真的不需要任何特殊的功能,除非有特定的用途 function concat(a,b){ if (arguments.length == 1) { return function(c){return a+c;};
concat('hello', 'world');
concat('hello')('world');
这两者都应该返回“helloworld”。这是怎么可能的?您可以使用
+
操作符来连接字符串。真的不需要任何特殊的功能,除非有特定的用途
function concat(a,b){
if (arguments.length == 1) {
return function(c){return a+c;};
} else if (arguments.length == 2) {
return a+b;
}
}
例如:
var string = 'hello, ' + 'world' + '!!!'
您可以只使用
+
操作符来连接字符串。真的不需要任何特殊的功能,除非有特定的用途
例如:
var string = 'hello, ' + 'world' + '!!!'
这将适用于问题中的规范,外加一点额外的:
function concat(/* varargs */) {
// Called with multiple arguments: concatenate them immediately
var originalArgs = [].slice.call(arguments);
if (originalArgs.length > 1) {
return originalArgs.join("");
}
// Called with zero or one arg: return a function that will perform the concatenation later
return function(/* varargs */) {
var newArgs = [].slice.call(arguments);
var combinedArgs = originalArgs.concat(newArgs);
return concat.apply(null, combinedArgs);
}
}
concat('a', 'b'); // 'ab'
concat('a')('b'); // 'ab'
concat('a', 'b', 'c'); // 'abc'
concat('a')('b', 'c'); // 'abc'
也就是说,它不会扩展超过两个调用(我认为不可能创建一个这样的函数),而且它散发出过度工程的味道。我会认真重新考虑您是否需要此功能。这将适用于问题中的规范,外加一点额外的:
function concat(/* varargs */) {
// Called with multiple arguments: concatenate them immediately
var originalArgs = [].slice.call(arguments);
if (originalArgs.length > 1) {
return originalArgs.join("");
}
// Called with zero or one arg: return a function that will perform the concatenation later
return function(/* varargs */) {
var newArgs = [].slice.call(arguments);
var combinedArgs = originalArgs.concat(newArgs);
return concat.apply(null, combinedArgs);
}
}
concat('a', 'b'); // 'ab'
concat('a')('b'); // 'ab'
concat('a', 'b', 'c'); // 'abc'
concat('a')('b', 'c'); // 'abc'
也就是说,它不会扩展超过两个调用(我认为不可能创建一个这样的函数),而且它散发出过度工程的味道。我会认真考虑你是否需要这个功能。还有另一种方法吗
function concat(){
var args = Array.prototype.slice.call(arguments).reduce(function(a,b){return a.concat(b)},[]),
internal = concat.bind(this,args);
internal.toString = function(){return args.join('');}
return internal
}
供使用
console.log(concat('hello', 'world')) //helloworld
console.log(concat('hello')('world')) // helloworld
console.log(concat('hello')('world','!!!')) //helloworld!!!
console.log(concat('hello')('world')('!!!')) //helloworld!!!
另一种方式
function concat(){
var args = Array.prototype.slice.call(arguments).reduce(function(a,b){return a.concat(b)},[]),
internal = concat.bind(this,args);
internal.toString = function(){return args.join('');}
return internal
}
供使用
console.log(concat('hello', 'world')) //helloworld
console.log(concat('hello')('world')) // helloworld
console.log(concat('hello')('world','!!!')) //helloworld!!!
console.log(concat('hello')('world')('!!!')) //helloworld!!!
那么just.join()呢?你知道如何
concat('hello')('world')代码>是否会表现?如果concat('hello')('world')('!!!),应该返回什么代码>或concat('hello','wonder')('world')('!!!')代码>我的代码由下面的函数concat(a,b){alert(a+b);}a='hello';b=‘ddfdf’;//concat(a,b);concat(a)(b);它返回“hellowundefined”,那么just.join()呢?你知道如何concat('hello')('world')代码>是否会表现?如果concat('hello')('world')('!!!),应该返回什么代码>或concat('hello','wonder')('world')('!!!')代码>我的代码由下面的函数concat(a,b){alert(a+b);}a='hello';b=‘ddfdf’;//concat(a,b);concat(a)(b);它返回“hellowundefined”+1看到concat('a')('b')的实现,我很惊讶……)+1看到concat('a')('b')脱帽的实现,我感到惊讶:)