在 angular.js 中,没有 () 的函数调用与调用 with() 有什么不同吗?
In angular.js , Is function call without () different from call with()?
我写了一些关于$timeout
服务的示例代码。
var myModule = angular.module('timerTest',[]);
myModule.controller('counting',function($scope,$timeout)
{
var timerObject;
var count =0;
var countTime = function()
{
count++;
console.log(count);
$scope.value = count;
timerObject = $timeout(countTime,1000);
};
$scope.startTimer = function()
{
console.log('timer start!');
$timeout(countTime,1000);
};
$scope.endTimer = function()
{
console.log('End Timer');
$timeout.cancel(timerObject);
};
});
在那个代码中 countTime
函数,当我写
timerObject = > $timeout(countTime(),1000);
它调用countTime()
非常快,因此它将使调用堆栈溢出。
但是当我写的时候
timerObject = $timeout(countTime,1000);
它工作得很好。有什么不同吗?
timerObject = $timeout(countTime(),1000)
立即在该行上调用countTime
,并将结果传递给$timeout
。每当你在函数名称后面加上括号时,这意味着你在那里调用函数 - 因为你在函数的每次迭代中都这样做,它会导致它无休止地重复,因此堆栈溢出。
另一方面,timerObject = $timeout(countTime,1000)
将countTime
函数本身传递给$timeout
- 这是使用该服务的正确方式,并且将导致$timeout
在大约 1000 毫秒后调用countTime
。
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 全局变量和全局对象的属性之间有什么区别吗
- Fighting with FRP
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- issue with FB.Event.subscribe
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 在ember/handlers中使用value和valueBinding有什么区别
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 在 angular.js 中,没有 () 的函数调用与调用 with() 有什么不同吗?
- XDomainRequest POST with XML..我做错了什么
- 此代码中发生了什么,如果没有“with”关键字,以下内容的基本实现是什么
- 拖动的最佳方式是什么?Drop with js,就像谷歌地图中的地图一样
- let块语句和等价的with语句之间的区别是什么
- 这个Javascript做什么`new Function(“_”,“at”,“with(_){return(“+text