用带参数的new替换旧函数(replace old function with new with arguments)

我有变量和函数的对象......以及新“function2”的定义

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; };

我想替换旧的getX函数的定义:

object.getX = function2(arg);

但是这段代码不是替换功能,而是设置新值

怎么做?

当我调用object.getX(“aa”)时,当我调用object.getX(15).... value“aa”时,我想要值“15”

I have object with variables and functions ... and definition of new "function2"

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; };

I want to replace definition of old getX function:

object.getX = function2(arg);

but this code is not replacing function, but setting new value

how to make it ?

I want value "15" when I call object.getX(15) .... value "aa" when I call object.getX("aa")

最满意答案

你必须为此目的使用bind(context,arguments..) ,

object.getX = function2.bind(object, arg);

如果您只是想在不设置任何上下文的情况下替换该function ( this ),

然后你可以简单地做到。

object.getX = function2;

你的完整代码是,

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; }; object.getX = function2.bind(object); console.log(object.getX("aa")); //"aa"

You have to use bind(context,arguments..) for this purpose,

object.getX = function2.bind(object, arg);

And if you just want to replace the function without setting any context(this),

Then you can do it simply by.

object.getX = function2;

Your full code would be,

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; }; object.getX = function2.bind(object); console.log(object.getX("aa")); //"aa"用带参数的new替换旧函数(replace old function with new with arguments)

我有变量和函数的对象......以及新“function2”的定义

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; };

我想替换旧的getX函数的定义:

object.getX = function2(arg);

但是这段代码不是替换功能,而是设置新值

怎么做?

当我调用object.getX(“aa”)时,当我调用object.getX(15).... value“aa”时,我想要值“15”

I have object with variables and functions ... and definition of new "function2"

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; };

I want to replace definition of old getX function:

object.getX = function2(arg);

but this code is not replacing function, but setting new value

how to make it ?

I want value "15" when I call object.getX(15) .... value "aa" when I call object.getX("aa")

最满意答案

你必须为此目的使用bind(context,arguments..) ,

object.getX = function2.bind(object, arg);

如果您只是想在不设置任何上下文的情况下替换该function ( this ),

然后你可以简单地做到。

object.getX = function2;

你的完整代码是,

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; }; object.getX = function2.bind(object); console.log(object.getX("aa")); //"aa"

You have to use bind(context,arguments..) for this purpose,

object.getX = function2.bind(object, arg);

And if you just want to replace the function without setting any context(this),

Then you can do it simply by.

object.getX = function2;

Your full code would be,

var object = { x: 11, y: 22, getX: function() { return this.x; } }; var function2 = function(value) { return value; }; object.getX = function2.bind(object); console.log(object.getX("aa")); //"aa"