- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
除了欺骗已经将 this
实现为某种东西的现有函数之外,您为什么要编写一个 javascript 函数以便您需要更改其上下文(通过 .call
或 .apply
) 而不是显式地将“上下文”作为另一个参数传递?有性能优势吗?
例子:
function tryIncrement(inc, context) {
context = context || this; // just so we can reuse same fn for the example
if( typeof context.val!= typeof 1|| typeof inc != typeof 1 ) return false;
context.val += inc;
return true;
}
var a = {name: 'A', val: 5}, b = {name: 'B', val: 20};
// reassign internal context
for(var i = 0, n = [1,3,"not a num",5]; i < n.length; i++) {
if( tryIncrement.call(a, n[i]) ) console.log('incremented', i, n[i], a);
else console.log('failed to increment', i, n[i], a);
}
// provide explicit context;
// could just as easily declared function so context was first param
// so it looked the same as previous implementation
for(var i = 0, n = [1,3,"not a num",5]; i < n.length; i++) {
if( tryIncrement(n[i], b) ) console.log('incremented', i, n[i], b);
else console.log('failed to increment', i, n[i], b);
}
最佳答案
在许多情况下,您可能希望使用 this
而不是传递额外的参数。例如考虑以下函数:
Function.prototype.async = function () {
setTimeout.bind(null, this, 0).apply(null, arguments);
};
此函数允许我们延迟函数调用,如下所示:
alert.async("This will display later.");
alert("This will display first.");
您可以在此处查看演示:http://jsfiddle.net/AjwQu/
我们可以将它作为参数传递,而不是将函数绑定(bind)到 this
:
function async(funct) {
setTimeout.bind(null, funct, 0).apply(null, [].slice.call(arguments, 1));
}
我们现在会像这样使用它:
async(alert, "This will display later.");
alert("This will display first.");
结果是一样的:http://jsfiddle.net/63dBF/
然而,要获取参数,我们必须使用 [].slice.call(arguments, 1)
代替。在第一个示例中,我们可以简单地使用 arguments
,因为函数不是参数列表的一部分。
凡事都有优点和缺点。你只需要知道什么时候使用什么。希望这能有所帮助。
好处:将使用 this
的函数转换为接受额外参数的函数非常容易,反之亦然。首先让我们定义一些实用函数:
var functProto = Function.prototype;
var bind = functProto.bind;
var bindable = bind.bind(bind);
var callable = bindable(functProto.call);
var appliable = bindable(functProto.apply);
bindable
函数允许您创建现有函数的可绑定(bind)版本,调用时返回绑定(bind)到给定参数的新函数。
callable
函数允许您创建现有函数的可调用版本,该函数在被调用时使用给定参数和 this
指针调用现有函数。
appliable
函数允许您创建现有函数的适用版本,该函数在调用时应用给定参数和指向现有函数的 this
指针。
然后给定第一个示例中的函数,我们可以创建第二个示例中的函数,如下所示:
var async = callable(functProto.async);
在此处查看演示:http://jsfiddle.net/3dSBS/
同样我们可以将第二个例子中的函数转换为第一个例子中的函数,如下所示:
Function.prototype.async = function () {
return async.apply(null, [this].concat([].slice.call(arguments)));
};
在此处查看演示:http://jsfiddle.net/rJQyS/
如您所见,使用 this
编写函数,然后构造接受上下文作为参数的函数,比反过来要容易得多。
关于Javascript:使用函数上下文与作为参数传递有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318904/
出现以下错误 Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable D
在调试应用程序时出现以下错误。 The CLR has been unable to transition from COM context 0x3b2d70 to COM context 0x3b2
在 GAE Go 中,为了记录,我们需要使用 appengine.NewContext(r) 创建一个新的上下文,它返回 context.Context。 如何使用此上下文在请求范围内设置/获取变量?
我想使用 Puppeteer 从放置在页面上 iframe 内的选择器中获取数据,该页面在与其父框架域不同的域上运行。因此,我不是任何域的所有者 - 无法使用 frame.postMessage。 试
我正在尝试获取可用的应用程序上下文并想切换到 webview 上下文,但 appium 仅获取 Navive App。 应用程序还启用了 WebView。 Appium 版本:1.10.1 Chrom
这个问题在这里已经有了答案: How to fix this nullOk error when using the flutter_svg package? (7 个回答) 7 个月前关闭。 当我尝
我观看了关于 Core Data 的 2016 WWDC 视频并查看了各种教程。我见过使用 Core Data Framework 创建对象以持久保存到 managedObjectContext 中的
这是代码 obj = { a: 'some value'; m: function(){ alert(this.a); } } obj.m(); 结果是'som
我正在尝试做类似的事情 $(".className").click(function() { $(this).(".anotherClass").css("z-index","1");
var User = { Name: "Some Name", Age: 26, Show: function() { alert("Age= "+this.Age)}; }; fun
我目前正在使用我见过的常见 Context 模式,它允许子组件通过传递修饰函数来更新父组件的状态(即 Provider)通过共享的 Context。 我遇到的问题是,修改函数只引用原始状态,不引用最新
有没有办法让 React Context类型安全与流类型? 例如: Button.contextTypes = { color: React.PropTypes.string }; 最佳答案 不幸
我想知道是否有一种方法可以为不同的功能使用不同的上下文类。 我希望有一个功能使用 MinkExtensions 进行浏览器测试,另一个功能使用和 HTTP 客户端(如 Guzzle)进行 API 测试
我有这个配置文件 apiVersion: v1 clusters: - cluster: server: [REDACTED] // IP of my cluster name: stag
我在实现非抢先式调度时遇到了用于初始化TCB的代码。 typedef struct TCB_t { struct TCB_t *next; struct TCB_t
我想将一个函数设置为数组中每个元素的属性,但使用不同的参数调用它。我想我会使用匿名函数来解决它: for ( var i = 0; i < object_count; i++ ) { obje
这个问题已经有答案了: How to access the correct `this` inside a callback (15 个回答) 已关闭 7 年前。 我正在做一些练习,但我在管道方法中丢
我正在尝试通过 Java 和 Android Studio 学习和制作 Android 应用程序。我对Java的了解程度是两年前几个小时的youtube学习和大学基础类(class)。不过我确实知道如
我在(这个)上遇到了问题。错误ImageView无法应用。我在 fragment 类中执行此代码。 ViewFlipper v_flipper; @Nullable @Override public
我想使用 openGL 的某些功能,但与渲染视觉内容无关。有没有办法在没有任何依赖性的情况下创建它(不是对 Windows,也不是某些包[SDL,SFML,GLUT])?只允许使用没有外部库的库,就像
我是一名优秀的程序员,十分优秀!