javascript 扩展功能 - 合并两个功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7286986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extend a function - merge two functions?
提问by Antony Carthy
I am in some new territory now, I haven't worked with this sort of thing in a while.
我现在在一些新领域,我已经有一段时间没有处理过这种事情了。
What is the best way to have a Javascript 'class' for instance
例如,拥有 Javascript“类”的最佳方法是什么
// In the parent instance
function xyz()
{
var x = 1;
}
I want to set this in the class, and when a user extends a class, I want them to effectively be extending this function. I know I'm not being clear, but it's been a long day. This is the user's code for instance.
我想在类中设置这个,当用户扩展一个类时,我希望他们有效地扩展这个功能。我知道我没有说清楚,但这是漫长的一天。例如,这是用户的代码。
// In the child instance
function xyz()
{
var y = 2;
}
Merging should result in:
合并应该导致:
// In the merged instance
function xyz()
{
var x = 1;
var y = 2;
}
Am I smoking the wrong stuff or asking the wrong question?
我是抽错了烟还是问错了问题?
回答by Matt
You can't 'merge' functions as you describe them there, but what you can do is have one function be redefined to call both itself and a new function (before or after the original).
你不能像你在那里描述的那样“合并”函数,但你可以做的是重新定义一个函数来调用它自己和一个新函数(在原始函数之前或之后)。
var xyz = function(){
console.log('xyz');
};
var abc = function(){
console.log('abc');
};
// and, elsewhere, if you want to merge:
var orig = abc;
abc = function(){
orig.call(this, arguments);
xyz.call(this, arguments);
};
The inclusion of (this, arguments) is not needed if you don't care about execution context or if the called function is parameterless. But I included for clarity of what one might do if you wanted a parameterized method.
如果您不关心执行上下文或被调用的函数是无参数的,则不需要包含 (this, arguments)。但是为了清楚起见,如果您想要一个参数化方法,我可能会做什么。
回答by ntangjee
You tag the question with jquery, so I assume you use jquery. with jquery, you could merge objects with jQuery.extend().
您使用 jquery 标记问题,因此我假设您使用 jquery。使用 jquery,您可以使用jQuery.extend()合并对象。
var object1 = {
apple: 0,
banana: {weight: 52, price: 100},
cherry: 97
};
var object2 = {
banana: {price: 200},
durian: 100
};
/* merge object2 into object1 */
$.extend(object1, object2);
or use prototype chain to implement inheritance. for example:
或者使用原型链来实现继承。例如:
function a() {
this.t1 = 1;
this.sayMyName = function() {
alert('a');
}
}
b.prototype = new a;
b.prototype.constructor = b;
function b() {
this.t2 = 2;
this.sayMyName = function() {
alert('b');
}
}
var obj = new b();
alert(obj.t1); // this would say 1
obj.sayMyName(); // this would say b
回答by Hymanstine
const mergeFunctions = function () {
let listOfFuncs = []
for (let func of arguments) {
listOfFuncs.push(func)
}
return function () {
for (let func of listOfFuncs) {
func(arguments)
}
}
}
let x = function () {
console.log("hello");
}
let y = function () {
console.log("world")
}
mergeFunctions(x, y)()
/////////////////////
hello
world
回答by keithhackbarth
If I understand you correctly, you can try renaming the original function and calling it within the new function:
如果我理解正确,您可以尝试重命名原始函数并在新函数中调用它:
// In the parent instance
function xyz()
{
var x = 1;
}
// In the child instance
var old_xyz = xyz;
function xyz()
{
var y = 2;
old_xyz();
}
Also works with class/method inheritance:
也适用于类/方法继承:
MyClass.prototype.old_xyz = MyClass.prototype.xyz;
MyClass.prototype.xyz = function () {
this.old_xyz();
}