在JavaScript中向函数中插入一行
在JavaScript中,是否可以在已经存在的函数中插入一行?我想创建一个函数,在函数中的特定位置插入一行:在JavaScript中向函数中插入一行,javascript,function,Javascript,Function,在JavaScript中,是否可以在已经存在的函数中插入一行?我想创建一个函数,在函数中的特定位置插入一行: function insertLine(theFunction, lineToInsert, positionToInsert){ //insert a line into the function after the specified line number } 例如,是否可以通过编程方式插入行checkParameterTypes(min,“string”,
function insertLine(theFunction, lineToInsert, positionToInsert){
//insert a line into the function after the specified line number
}
例如,是否可以通过编程方式插入行checkParameterTypes(min,“string”,max,“string”)代码>在此函数的第一行之前
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
是的,你可以,但使用eval总是有害的;)
函数插入器(arr、值、索引){
插入的var,i,newarr=[];
对于(i=0;i
编辑:
如问题注释中所述,这样做会使范围变宽。如果您希望函数开头出现问题,可以使用以下方法。您确实可以访问此
以及注入函数中的参数。因此,它仍然适用于需要特定上下文的函数
function inject(before, fn) {
return function(){
before.apply(this, arguments);
return fn.apply (this, arguments);
}
}
比如说
function add(a, b) {
return a + b;
}
function concat(a, b) {
return a + b;
}
/**
* You can repeat index and type to check multiple arguments
*/
function createArgumentChecker(index, type /**index, type, ... */) {
var originalArgs = arguments;
return function() {
for (var i=0; i < originalArgs.length; i+=2) {
var index = originalArgs[i],
requestedType = originalArgs[i+1],
actualType = typeof arguments[index];
if (typeAtIndex != actualType) {
console.log("Invalid argument passed at index " + index +
". Expected type " + requestedType + "but it's " + actualType );
}
}
}
}
function logArguments() {
console.log(this, arguments);
}
// Inject an argument checker
add = inject(add, createArgumentChecker(0,"number", 1, "number"));
concat = inject (concat, createArgumentChecker(0, "string", 1, "string"));
// You can even do it multiple times, inject an argument logger;
add = inject(add, logArguments);
concat = inject(concat, logArguments);
功能添加(a、b){
返回a+b;
}
函数concat(a,b){
返回a+b;
}
/**
*可以重复索引和键入以检查多个参数
*/
函数createArgumentChecker(索引,类型/**索引,类型,…*/){
var originalArgs=参数;
返回函数(){
对于(变量i=0;i
这在调试无法修改源代码的网站时非常方便,我不会使用它进行参数检查,除非您可以将其剥离用于生产版本。我真的怀疑您是否可以在函数中“插入”行。但是,当然,您可以将该函数包装到另一个调用您的checkParameterTypes(..)
的函数中。在调用getRandomInteger(..)
@struchMaster之前,您肯定可以通过获取函数的代码、放入并重新定义它来插入一行。但你为什么要这么做?正如您所建议的那样,鸭子打孔是实现这一点的方法。@Bradeval
/函数
构造函数将在全局范围内对其求值,从而失去对所有局部变量的访问。当然,它可能满足某些用例,但并不理想。@fabriciomatté你说的“在全球范围内评估它”是什么意思?我想没有。你在想setTimeout
?@fabriciocomattéYep,我今天学习了函数的作用域,所以我们都学习了:)但是,是的,我会避免两者,你是对的,这很麻烦,我只想指出,可以通过访问局部变量来实现。不管是哪种方式,这里被接受的答案似乎很好,并且没有使用任何一种:)
function add(a, b) {
return a + b;
}
function concat(a, b) {
return a + b;
}
/**
* You can repeat index and type to check multiple arguments
*/
function createArgumentChecker(index, type /**index, type, ... */) {
var originalArgs = arguments;
return function() {
for (var i=0; i < originalArgs.length; i+=2) {
var index = originalArgs[i],
requestedType = originalArgs[i+1],
actualType = typeof arguments[index];
if (typeAtIndex != actualType) {
console.log("Invalid argument passed at index " + index +
". Expected type " + requestedType + "but it's " + actualType );
}
}
}
}
function logArguments() {
console.log(this, arguments);
}
// Inject an argument checker
add = inject(add, createArgumentChecker(0,"number", 1, "number"));
concat = inject (concat, createArgumentChecker(0, "string", 1, "string"));
// You can even do it multiple times, inject an argument logger;
add = inject(add, logArguments);
concat = inject(concat, logArguments);