在标签之间拆分Javascript代码 [英] Split Javascript Code Between Tags

查看:68
本文介绍了在标签之间拆分Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是可能的,因为我已经尝试过了,但有没有办法让你的代码基本上这样:



I doubt it's possible as I've tried, but is there a way to essentially have your code like this:

<script>
function(){
//do stuff
</script>
<script>
//do more stuff
}
</script>



看起来好像这样将会很有用,但也许不是另一个脚本标签我会添加一个js文件,由PHP编辑,以添加更多的东西到函数....


This may seem like it would be useful to do, but maybe instead of another script tag i would add a js file that would be edited by PHP to add more stuff to the function....

推荐答案

不是说它是否可能,它只会是无用的,或者说是有害的。



JavaScript规则比你想要的更合理(更准确地说,是什么)你可能认为自己想要,因为我无法想象清醒头脑中任何一个人会有意识地想要这么奇怪的事情。您可以在一个脚本中创建一些对象,并在另一个脚本中使用它,无论如何,对象定义或语句(调用,赋值等)之间的边界。函数定义与任何其他对象定义没有区别,因为函数是一等公民



此外,你应该尝试实现恰恰相反:即使使用单个脚本,代码也应该尽可能地构建,并且应该尽可能地将代码隐藏在代码部分中。例如,某些对象仅在某个函数内部使用,它应该是本地的,即使这是一个函数(允许内部函数并且对于更好的封装很重要。对象不应该通过外部上下文但作为函数参数传递...等等。



-SA
Not that it's possible or not, it would simply be useless or, rather, harmful.

JavaScript rules are much more reasonable than what you want (more exactly, what you probably think you want, as I cannot imagine that any one in sober mind would consciously want such a weird thing). You can create some object in one script and work with it in another, anyway, the boundary between object definition or statement (call, assignment, and the like). And a function definition is no different from any other object definition, as functions are first-class citizen.

Moreover, you should try to achieve just the opposite: even withing a single script, the code should be structured as much as possible, and the detail should be hidden from the parts of code where they are not used as well as possible. For example, of some object is used only inside some function, it should be made local, even if this is a function (inner functions are allowed and are important for better encapsulation. Objects should not be shared through the outer context but passed as function parameters and… so on.

—SA


你不能分割一半的功能(我不知道任何允许这样的东西的语言)...

如果你需要一些动态功能,以某种方式扩展现有的你必须创建一个其他的解决方案...

有一个很好的JavaScript库可以这样做: http://extendjs.org/ [< a href =http://extendjs.org/target =_ blanktitle =New Window> ^ ]
You can not split a function in the half (I do not know any language that allow such a thing)...
If you need some dynamic functionality that somehow extend the existing you have to create an other solution...
There is a nice JavaScript library to do so: http://extendjs.org/[^]


编写这样的JavaScript是一个坏主意除了这里提供的好解决方案,我会说要学习不引人注目的javascript实现,而不是将所有内容混合在一起r网页。
Writing JavaScript like this is a bad idea. Apart from the good solutions provided here I would say to learn about unobstrusive javascript implementation instead of mixing up everything together in your web page.


这篇关于在标签之间拆分Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆