命名空间和全局变量 [英] Namespaces and global variables

查看:438
本文介绍了命名空间和全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavascriptMVC(你不需要知道它!)和他们的库窃取管理文件依赖。

I'm using JavascriptMVC (you don't need to know it!) and their library "steal" that manages file dependencies.

我是初学者使用javascript,并且我没有使用命名空间;我需要一些由PHP初始化的全局变量,这些变量将用于很多其他JS文件,这就是为什么我想让它们全局化:

I'm a beginner with javascript and there's something I don't get with namespaces; I need some global variables intialized by PHP, these variables will be used in a lot of other JS files, that's why I want to make them global:

index .php

<script type="text/javascript">
steal('jquery', function() {
   // here is some jquery specific code
   var appletVersion = '<?php echo $appletVersion; ?>';
   var baseUrl = '<?php echo BASE_URL; ?>';
});
</script>

在我的JS文件中,我无法访问这两个变量,因为我已经窃取了(' jquery',function(){...});而且我猜他们在那个街区之外是不可见的。

In my JS files, I can't access these two variables since I've put steal('jquery', function(){ ... }); and I guess they are unvisible outside of that block.

test.js

steal('jquery', function(){
   console.log(baseUrl);  // error
});


推荐答案

那是因为你的两个变量是你的函数的本地变量传递给偷。我的建议是始终将全局变量称为 window.globalName 以使您的意图明确。

That's because your two variables are local to your function that is passed to steal. My suggestion is to always refer to globals as window.globalName to make your intention clear.

steal('jquery', function() {
   // here is some jquery specific code
   window.appletVersion = '<?php echo $appletVersion; ?>';
   window.baseUrl = '<?php echo BASE_URL; ?>';
});
// Now you can access window.appletVersion anywhere in your code

请注意,有无需等待窃取来抓取jQuery来初始化这些变量,因此您可以在外部(在全局范围级别)执行此操作。

Note that there is no need to wait for steal to grab jQuery to initialize those variables, so you could do that outside (at the global scope level).

命名空间

比使用更好的解决方案window.globalName 是在全局级别创建自己的命名空间,以便可以将全局命名空间污染限制为单个对象。这将有助于调试,因为所有代码都不会与全局对象上的其余属性混合。只需console.log它,你就可以看到所有你自己的全局变量。

An even better solution than using window.globalName is to create your own namespace at the global level so that you can restrict your global namespace pollution to a single object. This will help when debugging since all your code will not be mixed with the rest of the properties on the global object. Just console.log it, and you'll have all your own globals to look at.

var myNs = {}; // Put all your globals, classes, functions in here to avoid conflicts.
myNs.appletVersion = '<?php echo $appletVersion; ?>' ;

在JS中打印PHP值

当你在PHP中有一个值并且想要在页面上将它作为JS变量打印时,你应该使用 json_encode 。如果您的字符串嵌入了换行符,引号甚至二进制数据,那么您将不会遇到任何问题。您甚至不必担心类型, json_encode 输出一直有效用于JavaScript的东西

When you have a value in PHP and you want to print it on the page as a JS variable, you should use json_encode. Then you won't have problems with if your string has embedded newlines, quotes or even binary data. You don't even have to worry about the type, json_encode outputs something that is always valid to be used in JavaScript

myNs.appletVersion = <?php echo json_encode($appletVersion); ?>;
myNs.baseUrl = <?php echo json_encode(BASE_URL); ?>;

这篇关于命名空间和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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