Javascript 获取类(非对象)的函数引用
我有一个代码,其中一个函数用于修改现有函数并返回一个新函数引用。我希望该函数应用于类的特定方法。 我现在的代码是Javascript 获取类(非对象)的函数引用,javascript,object,Javascript,Object,我有一个代码,其中一个函数用于修改现有函数并返回一个新函数引用。我希望该函数应用于类的特定方法。 我现在的代码是 function modifyMethod(func) { return function() { console.log('working'); return func.apply(this, arguments); }; } function modifyClassMethods(ClassName, methodArray) { // The co
function modifyMethod(func) {
return function() {
console.log('working');
return func.apply(this, arguments);
};
}
function modifyClassMethods(ClassName, methodArray) {
// The code goes here
return ClassName;
}
class Temp {
hi() {
console.log("hi method");
}
}
Temp = modifyClassMethods(Temp, ["hi"]);
const temp = new Temp();
// This should print
//
// working
// hi method
temp.hi();
当我尝试使用Temp.hi
调用方法modifyMethod
时,func
未定义。如果我创建一个对象,然后修改该方法,那么新方法将仅应用于该特定对象的方法,而不是该特定类的所有对象
请注意,这只是一个示例。我想将此修改应用于多个类的方法。因此,我也不能概括方法名。
modifyClassMethods
的任何代码片段都会很有帮助。在类
构造体中使用方法语法定义的、未标记为static
的方法是原型方法,因此它们位于Temp.prototype
上,而不是Temp
本身。这就是你要更新它们的地方:
Temp.prototype.hi = modifyMethod(Temp.prototype.hi);
只有静态方法在Temp
本身上结束
您可能会看到在类中使用的语法创建的其他函数:
class Temp {
hi = () => {
//
};
}
这些是实例方法。它们是由构造函数创建的,并为每个实例重新创建,大致就像它们是这样编写的:imk
class Temp {
constructor() {
this.hi = () => {
//
};
}
}
除非创建了实例,否则无法包装这些内容,因为它们是特定于实例的
最后,考虑一下:
class Temp {
static staticMethod() {
// ...
}
prototypeMethod() {
// ...
}
instanceMethod = () => {
// ...
};
constructor() {
this.anotherInstanceMethod = () => {
// ...
};
this.yetAnotherInstanceMethod = function {
// ...
};
}
}
该类显示了三种类型的方法:
- 静态方法,如
静态方法
,可在Temp
上找到(例如Temp.staticMethod
)李>
- 原型方法,例如
prototypeMethod
,您可以在Temp.prototypeMethod
上找到它(例如,Temp.prototypeMethod
);及
- 实例方法,例如
实例方法
,另一种实例方法
,以及yetAnotherInstanceMethod
,如果/当创建任何实例时,您可以在实例本身上找到这些方法
从技术上讲,它们是用对象创建的。defineProperty
如下所示:
class Temp {
constructor() {
Object.defineProperty(this, "hi", {
value: () => {
//
},
writable: true,
configurable: true,
enumerable: true
});
}
}
…不是通过简单的分配。我在示例中使用了简单赋值来保持它…简单。:-) 在类
构造体中使用方法语法定义的、未标记为静态
的方法是原型方法,因此它们位于临时原型
上,而不是临时
本身。这就是你要更新它们的地方:
Temp.prototype.hi = modifyMethod(Temp.prototype.hi);
只有静态方法在Temp
本身上结束
您可能会看到在类中使用的语法创建的其他函数:
class Temp {
hi = () => {
//
};
}
这些是实例方法。它们是由构造函数创建的,并为每个实例重新创建,大致就像它们是这样编写的:imk
class Temp {
constructor() {
this.hi = () => {
//
};
}
}
除非创建了实例,否则无法包装这些内容,因为它们是特定于实例的
最后,考虑一下:
class Temp {
static staticMethod() {
// ...
}
prototypeMethod() {
// ...
}
instanceMethod = () => {
// ...
};
constructor() {
this.anotherInstanceMethod = () => {
// ...
};
this.yetAnotherInstanceMethod = function {
// ...
};
}
}
该类显示了三种类型的方法:
- 静态方法,如
静态方法
,可在Temp
上找到(例如Temp.staticMethod
)李>
- 原型方法,例如
prototypeMethod
,您可以在Temp.prototypeMethod
上找到它(例如,Temp.prototypeMethod
);及
- 实例方法,例如
实例方法
,另一种实例方法
,以及yetAnotherInstanceMethod
,如果/当创建任何实例时,您可以在实例本身上找到这些方法
从技术上讲,它们是用对象创建的。defineProperty
如下所示:
class Temp {
constructor() {
Object.defineProperty(this, "hi", {
value: () => {
//
},
writable: true,
configurable: true,
enumerable: true
});
}
}
…不是通过简单的分配。我在示例中使用了简单赋值来保持它…简单。:-) 谢谢你宝贵的回答。但是,如果该方法是使用箭头函数创建的,即hi=()=>{}
,则Temp.prototype.hi不起作用。@NikhilGoyal-这些不是方法本身,它们是构造函数分配给实例的箭头函数。除非创建了实例,否则它们不存在,除非创建了实例,否则无法包装它们。@NikhilGoyal-很乐意帮忙!我已经更新了答案,提出了这三种方法。@NikhilGoyal-如果我没有在我的书的第4章和第18章中提到这一切,那我就是失职了;我的个人资料中的详细信息。;-)谢谢你宝贵的回答。但是,如果该方法是使用箭头函数创建的,即hi=()=>{}
,则Temp.prototype.hi不起作用。@NikhilGoyal-这些不是方法本身,它们是构造函数分配给实例的箭头函数。除非创建了实例,否则它们不存在,除非创建了实例,否则无法包装它们。@NikhilGoyal-很乐意帮忙!我已经更新了答案,提出了这三种方法。@NikhilGoyal-如果我没有在我的书的第4章和第18章中提到这一切,那我就是失职了;我的个人资料中的详细信息。;-)