标签: function-binding

ES6箭头函数和与Function.prototype.bind绑定的函数之间有什么区别(如果有的话)?

在我看来,在ES6,下面的两个功能都非常接近相同的:

function () {
  return this;
}.bind(this);

() => {
  return this;
};
Run Code Online (Sandbox Code Playgroud)

最终结果看起来是一样的:箭头函数生成一个JavaScript函数对象,其this上下文绑定到与this创建它们的位置相同的值.

显然,在一般意义上,Function.prototype.bind它比箭头函数更灵活:它可以绑定到本地以外的值this,并且它可以this在任何时间点绑定任何函数,可能在最初创建之后很长时间.不过,我不问如何bind本身就是从箭头的功能不同,我问箭头的功能是如何从立即调用不同bind使用this.

ES6中的两个结构之间是否有任何差异?

javascript ecmascript-6 function-binding arrow-functions

47
推荐指数
3
解决办法
1万
查看次数

console.log()在控制台以外的对象上调用

我记得,当我想console.log作为一个回调参数传递给某个函数时,除非我使用该bind()方法绑定console它,否则它不起作用.

例如:

const callWithTest = callback => callback('test');
callWithTest(console.log); // That didn't use to work.
callWithTest(console.log.bind(console)); // That worked (and works) fine.
Run Code Online (Sandbox Code Playgroud)

请参阅未捕获的TypeError:javascript中的非法调用.

但是,最近我发现console.log()即使调用除控制台以外的对象,它也能正常工作.例如:

console.log.call(null, 'test');
Run Code Online (Sandbox Code Playgroud)

日志'test'.

它何时以及为何会改变?规范是否说明了什么?

javascript this function-binding console.log

32
推荐指数
1
解决办法
913
查看次数

JSX道具不应该使用.bind()

当我以这种方式绑定时如何修复此错误:先前在构造函数中绑定已解决,但这对我来说有点复杂:

    {this.onClick.bind(this, 'someString')}>
and
     <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
Run Code Online (Sandbox Code Playgroud)

jsx function-binding reactjs react-jsx

16
推荐指数
2
解决办法
1万
查看次数

Function.bind.bind(Function.call)如何解决?

我的代码库中有这一行:

var uncurryThis = Function.bind.bind(Function.call);
Run Code Online (Sandbox Code Playgroud)

我正在努力解决这个问题.据推测,它是不可靠的.我该如何解决这个问题?

我想这是一个版本Function.bind,其自身this必然Function.call.对我没有帮助.而且我没有找到任何用途,所以我甚至不确定你是单独称它还是需要"作为一种方法"来调用它,只是,你知道,首先绑定它.

javascript functional-programming this function-binding

13
推荐指数
2
解决办法
1662
查看次数

在JavaScript事件回调中绑定"this"的正确方法?

我创建了一个名为SearchBox的类来处理搜索交互(延迟触发,搜索输入键按下,防止搜索活动时,搜索完成时同步结果,文本更改等).

所有类方法都是原型方法,意味着可以通过访问this.在下面的代码中,假设p是类的原型.

p.registerListeners = function () {
    $(this.element).on('keypress', this.searchKeyPressed);
};

p.unregisterListeners = function () {
    $(this.element).off('keypress', this.searchKeyPressed);
};
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为当keypress事件调用searchKeyPressed处理程序时,它不会在上下文中这样做this.我能想到的唯一解决方案是只有现代浏览器支持的解决方案,即绑定回调this,实际创建一个新函数.由于它创建了一个新函数,我必须将其缓存以便以后删除它,因为我必须将相同的引用off传递给我传递给它的函数on.

有没有比这更好的方法,或者这样可以吗?

var boundKeyPressed;

p.registerListeners = function () {
    boundKeyPressed = this.searchKeyPressed.bind(this);
    $(this.element).on('keypress', boundKeyPressed);
};

p.unregisterListeners = function () {
    $(this.element).off('keypress', boundKeyPressed);
};
Run Code Online (Sandbox Code Playgroud)

我认为这可能jQuery.on会提供一种自动执行此事件绑定的方法,但它似乎会this根据它的调用方式绑定到不同的事物.例如,在使用时on('eventname',instance.func),this是"currentTarget"(在冒泡术语中不一定是"目标"),而在使用时on('eventname','selector',instance.func),this是指与选择器匹配的元素.在任何一种情况下,func运行就好像它没有关系instance.

javascript jquery jquery-callback function-binding

8
推荐指数
2
解决办法
9668
查看次数

Java lambda可以将方法绑定到它们的参数吗?

下面讨论如何使用lambdas将方法作为参数传递:
Java Pass Method as Parameter

在其他语言中,即C++,可以使用Lambdas将函数绑定到它的参数 - 这里讨论:
Bind Vs Lambda?

在Java中,是否可以使用lambdas绑定方法?

如果是这样,你会如何做到这一点?

编辑>>>>

根据要求,我通常尝试做的一个例子:

请注意,这里有伪代码.

public class DataView {

    private static ArrayList<Float> rectData = new ArrayList<Float>();
    private static ArrayList<Float> textData = new ArrayList<Float>();

    DataView(){

        //Pseudo Code:
        boundFunction mybind  = boundFunction(functionA, 5, 10);
        boundFunction mybind2 = boundFunction(functionB, 10, 12);

        iter(mybind);
        iter(mybind2);

    }

    //Method with pseudo parameter
    private void iter(functionSignature){
        for(Float i : textData){

            //Pseudo call to pseudo parameter
            functionSignature();

        }
    }

    private void functionA(int a, int b){ …
Run Code Online (Sandbox Code Playgroud)

java lambda function-pointers function-binding

8
推荐指数
1
解决办法
5927
查看次数

为什么我不能在开放式工具打开的情况下直接绑定IE9上的console.log?

在IE9中打开开发人员工具,此代码可以正常工作:

var log = Function.prototype.bind(console.log, console);
Run Code Online (Sandbox Code Playgroud)

但如果我输入

console.log(console, console.log);
var log = console.log.bind(console);
Run Code Online (Sandbox Code Playgroud)

然后我明白了:

SCRIPT438:对象不支持属性或方法'bind'

为什么?

这是一个已知的IE错误还是正常行为?

它是否影响其他功能(我没有问题window.alert哪个也是原生的)?

javascript console internet-explorer function-binding

7
推荐指数
2
解决办法
1648
查看次数

如何在 TypeScript 中绑定泛型函数

我正在尝试将通用函数绑定到特定的上下文和参数,但是我似乎无法找出正确的方法来做到这一点。

这是我的代码:

interface A {}

interface Repository<T> {
  save(item: T): boolean;
} 

const updater = <T>(repo: Repository<T>, item: T) => {
  // do things and update
};

const items: A[] = [];
const repo: Repository<A> = {
  save(item: A) {
    /* do things */
    return true;
  }
}

items.forEach(updater<A>.bind(null, repo)); // Line with issue
Run Code Online (Sandbox Code Playgroud)

这里的想法是有一个通用函数,它将存储库作为第一个参数,当绑定时将创建一个适合在 forEach 中使用的函数。但是我得到了像( expected和这样的错误cannot find name 'bind'

我不完全确定我的语法是否有问题,或者我遗漏了什么。

我也试过:

updater.bind(null, repo)
Run Code Online (Sandbox Code Playgroud)

但是这不适用于 3.2 选项,strictBindCallApply因为它假定类型参数T={}

有谁知道正确的方法来做到这一点?

游乐场链接

generics typescript function-binding

7
推荐指数
0
解决办法
1052
查看次数

解释bindbind()函数

有人可以解释这个功能吗?

var bindbind = Function.prototype.bind.bind(Function.prototype.bind);
Run Code Online (Sandbox Code Playgroud)

我理解它产生的结果:

var bindedContextFunc = bindbind(function)(context);
bindedContextFunc(args);
Run Code Online (Sandbox Code Playgroud)

但是不明白创建这个功能的过程,我的意思是部分 bind(Function.prototype.bind)

javascript functional-programming function-binding

6
推荐指数
1
解决办法
228
查看次数

如何将双参数函数"转换"为单参数函数?

在matlab中,人们可以写:

S = @(x,y) x^2+y^2-1
G = @(x) S(x,1);
Run Code Online (Sandbox Code Playgroud)

如果我有一个期望单参数函数的函数,我可以做到以上几点.我怎么能用c/c ++做到这一点?

我有一个库函数(来自CGAL库),它希望作为一个参数,一个函数本身只有一个参数.理想情况下,我有一个class(SphericalHarmonics),我希望有一个成员函数,它接受一个参数.所以我有:

FT SphericalHarmonics::distFunction(Point_3 p)
Run Code Online (Sandbox Code Playgroud)

(注意这FT是一个类似的类型double)但当然我尝试的时候

SphericalHarmonics *sh = new SphericalHarmonics(1);
Surface_3 surface(sh->distFunction, Sphere(ORIGIN,2.));
Run Code Online (Sandbox Code Playgroud)

this也被视为参数,我的distFunction函数是一个双参数函数,并抛出一个错误.

请注意,这可以通过全局变量来解决,即

SphericalHarmonics *sh;
FT dist_function(Point_3 p) {
    return sh->distFunction(p);
}

main() {
    sh = new SphericalHarmonics(1);
    Surface_3 surface(dist_function);
}
Run Code Online (Sandbox Code Playgroud)

但是,这真的不理想.我想要一种没有全局变量的方法,因为能够拥有一个可以轻松地与CGAL库集成的类函数会好得多.

提前致谢!

[更新]

@安迪警车:我想你std::bindlambda解决方案,但似乎仍然运行到错误,至于参数的个数.

main我在使用代码时:

SphericalHarmonics *sh = new SphericalHarmonics(cInit, numL, symm);
auto fxn = std::bind(&SphericalHarmonics::distFunction, sh, std::placeholders::_1);
Surface_3 surface(fxn, Sphere_3(ORIGIN,2.)); …
Run Code Online (Sandbox Code Playgroud)

c c++ matlab lambda function-binding

5
推荐指数
2
解决办法
350
查看次数