将'this'变量从javascript'String'函数绑定到另一个addon函数

时间:2022-01-23 20:35:24

I am trying to bind this function

我试图绑定这个功能

 var addon = function(a){
        if (typeof a == "string"){ 
            return this+a;

        }else{ 
            return this+String(a||"");
        }

    }

To

String.prototype.addon

So that when I call

所以当我打电话时

"I".addon(" remember.");

It returns

"I remember."

This is what I have for the String.prototype.addon so far.

到目前为止,这就是我对String.prototype.addon的看法。

String.prototype.addon = addon.bind();

I can't figure out what to pass through the bind as the this variable, can anyone figure it out?

我无法弄清楚通过绑定传递什么作为这个变量,任何人都可以搞清楚吗?

2 个解决方案

#1


It's fairly easy, you're just calling the function instead of assigning it.

这很简单,你只是调用函数而不是分配它。

Try

String.prototype.addon = addon;

Then

"I".addon(" remember.");
> "I remember."

#2


Bind creates a new function that will have this set to the first parameter passed to bind().

Bind创建一个新函数,将其设置为传递给bind()的第一个参数。

Here's an example that shows how to use bind to pass a member method around that has the correct this:

这是一个示例,演示如何使用bind传递成员方法,具有正确的:

var Button = function(content) { 
  this.content = content;
};
Button.prototype.click = function() {
  console.log(this.content + ' clicked');
}

var myButton = new Button('OK');
myButton.click();

var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton

var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton

Which prints out:

打印出来:

OK clicked
undefined clicked
OK clicked

You can also add extra parameters after the 1st parameter and bind will pass in those values to the original function before passing in the extra parameters you pass to the bound function:

您还可以在第一个参数之后添加额外的参数,bind将传递这些值到原始函数,然后传递传递给绑定函数的额外参数:

// Example showing binding some parameters
var sum = function(a, b) {
  return a + b;
};

var add5 = sum.bind(null, 5);
console.log(add5(10));

Which prints out: 15

打印出来:15

Source, and some examples: JavaScript Function bind

源码和一些例子:JavaScript函数绑定

#1


It's fairly easy, you're just calling the function instead of assigning it.

这很简单,你只是调用函数而不是分配它。

Try

String.prototype.addon = addon;

Then

"I".addon(" remember.");
> "I remember."

#2


Bind creates a new function that will have this set to the first parameter passed to bind().

Bind创建一个新函数,将其设置为传递给bind()的第一个参数。

Here's an example that shows how to use bind to pass a member method around that has the correct this:

这是一个示例,演示如何使用bind传递成员方法,具有正确的:

var Button = function(content) { 
  this.content = content;
};
Button.prototype.click = function() {
  console.log(this.content + ' clicked');
}

var myButton = new Button('OK');
myButton.click();

var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton

var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton

Which prints out:

打印出来:

OK clicked
undefined clicked
OK clicked

You can also add extra parameters after the 1st parameter and bind will pass in those values to the original function before passing in the extra parameters you pass to the bound function:

您还可以在第一个参数之后添加额外的参数,bind将传递这些值到原始函数,然后传递传递给绑定函数的额外参数:

// Example showing binding some parameters
var sum = function(a, b) {
  return a + b;
};

var add5 = sum.bind(null, 5);
console.log(add5(10));

Which prints out: 15

打印出来:15

Source, and some examples: JavaScript Function bind

源码和一些例子:JavaScript函数绑定