JavaScript的event handler和bind function里对this的绑定 |
您所在的位置:网站首页 › rap的同义词 › JavaScript的event handler和bind function里对this的绑定 |
JavaScript的一大特点是函数是闭包(closure),也就是函数不但包括函数体,还包括了函数的环境。比如ChakraCore里面代表一遍用户定义的函数的ScriptFunction继承自ScriptFunctionBase和JavaScriptFunction来表示函数的静态代码,还包括了一个FrameDisplay* enviornment的成员表示环境(其实就是一个scope的数组)。闭包的环境是有词法作用域决定(Lexical Scope)的,也就是函数定义的位置决定了闭包的内容,但是this除外。this的内容是由调用方决定,属于动态作用域(Dynamic Scope)。比如func(),obj.func(),new func()针对同一个函数的三种调用方式,函数里面的this内容也不相同。 除了上面三种方式,this还有两种比较特殊的绑定,分别是在JavaScript的event handler和Function.prototype.bind()方法里面。 在HTML DOM里面event handler可以通过document.addEventListener来注册。在相应事件(比如button上的click时间)发生时,浏览器会调用注册的event handler,调用前会把this绑定到事件发生的元素(比如前面的button)。在Chrome浏览器里面对事件方法的调用实现在CallListenerFunction。精简版如下,可以看到在event handler/listener之前从event获取receiver传给调用handler函数作为this。 v8::Local V8EventListener::CallListenerFunction( ScriptState* script_state, v8::Local js_event, Event* event) { v8::Local handler_function = GetListenerFunction(script_state); v8::Local receiver = GetReceiverObject(script_state, event); ... v8::Local parameters[1] = {js_event}; v8::Local result; if (!V8ScriptRunner::CallFunction(handler_function, frame->GetDocument(), receiver, arraysize(parameters), parameters, script_state->GetIsolate())另外还有一类特殊的this绑定是调用Function.prototype.bind()返回的函数。比如func.bind(obj),返回的是一个新的函数对象。在ChakraCore里面这个返回的函数对象由BoundFunction表示而不是原来func对应的ScriptFunction。BoundFunction如下所示,不但包括了原来的函数对象targetFunction,还有boundThis和boundArgs,可以由用户的JavaScript代码提前完成this和参数的绑定。 class BoundFunction : public JavascriptFunction { ... static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...); virtual JavascriptString* GetDisplayNameImpl() const override; ... private: static FunctionInfo functionInfo; RecyclableObject* targetFunction; Var boundThis; uint count; Var* boundArgs; };上面的静态方法BoundFunction::NewInstance在ChakraCore初始化的时候被注册为BoundFunction的Type对象的入口点(entry point),在这个BoundFunction(func.bind(obj)返回的函数对象)被调用的时候,实际被调用的是这里的NewInstance方法。 if (config->IsES6FunctionNameEnabled()) { boundFunctionType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, BoundFunction::NewInstance, GetDeferredFunctionTypeHandler(), true, true); } else { boundFunctionType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, BoundFunction::NewInstance, SimplePathTypeHandler::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true); }下面是只绑定this没有参数的情况(boundFunction->count > 0), 当前boundFunction对象里面事先绑定的boundthis成员存放到了参数数组actualArgs的最开始的位置,被调用的函数会把这第一个参数当成this的内容。这也是ChakraCore里面JavaScript函数和C++函数的调用约定,JavaScript的函数都是动态的,不能生成一个像C++函数的静态声明给C++函数调用,所以采用了C++可变参数的声明形式,而可变参数的第一个传的就是JavaScript的this对象。 Var BoundFunction::NewInstance(RecyclableObject* function, CallInfo callInfo, ...) { BoundFunction *boundFunction = (BoundFunction *) function; Var targetFunction = boundFunction->targetFunction; Var newVarInstance = nullptr; Js::Arguments actualArgs = args; if (boundFunction->count > 0) { ...//参数绑定 } else { if (!(callInfo.Flags & CallFlags_New)) { actualArgs.Values[0] = boundFunction->boundThis; } } RecyclableObject* actualFunction = RecyclableObject::FromVar(targetFunction); Var aReturnValue = JavascriptFunction::CallFunction(actualFunction, actualFunction->GetEntryPoint(), actualArgs);到这里两种特殊的绑定类型也就比较清楚了。最开始看这个问题是因为想知道这两种绑定谁的优先级更高,因为可以把bound function注册为event handler/listener。当然可以直接在浏览器里面实验,不过看看代码更有助于理解。根据上面的代码,event handler调用的时候会显示传递receiver对象作为this,但是进入了bound function入口点后,会从bound function获取缓存的this对象去调用实际的函数,而调用event handler/listener传递的receiver就直接扔掉了,所以boundFunction对this的绑定优先级更高。 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |