我有一页有一堆....部分。在其中一个中,我完成了一半并决定我要停止,而不是运行此脚本标记的其余内容 - 但仍运行页面上的其他代码段。有没有办法在不将整个代码段包装在函数调用中的情况下做到这一点?
例如:
<script type='text/javascript'>
console.log('1 start');
/* Exit here */
console.log('1 end');
</script>
<script type='text/javascript'>
console.log('2 start');
console.log('2 end');
</script>
这应该产生输出
1 start
2 start
2 end
而不是1 end
。
显而易见的答案是将脚本包装在一个函数中:
<script type='text/javascript'>
(function(){
console.log('1 start');
return;
console.log('1 end');
})();
</script>
虽然这通常是最好的方法,但也有不适合的情况。所以我的问题是,如果有的话,还有什么其他方法可以做到这一点?或者如果不是,为什么不呢?