日期:2014-05-16  浏览次数:20346 次

js解析顺序,求科普
<script type="text/javascript">
var Page={
//开发环境
host:"localhost:8080/webgame1/",
//运行环境
//host:"42.120.16.151:8080/webgame1/",
webHost:"http://"+Page.host,
chatHost:"ws://"+Page.host+"ChatServlet.do?channelId=",
type:"POST",
dataType:"json",
gameId:"",
//返回数据
returnData:"",
currentPage:"",
asyncProcess:function(_url,_data,_callback){
$.ajax({
type:this.type,
url:_url,
dataType:this.dataType,
data:_data,
success:_callback,
});
}
}
$(document).ready(function(){
alert(Page.webHost);
});
</script>


为什么打印出来的是undefined呢?这个问题导致我发异步请求的时候是错误的路径。

------解决方案--------------------
var Page = {
//...
host: "localhost:8080/webgame1/",

//webHost:"http://"+Page.host, /*TypeError: Page is undefined*/

webHost:"http://localhost:8080/webgame1/", // 硬编码或使用getWebHost方法
getWebHost: function(){return "http://" + this.host},
//...
}

alert(Page.webHost)
alert(Page.getWebHost())
------解决方案--------------------
host没有声明,改成new的吧。
var Page = function()
{
this.host = ..;
this.data = ...;
this.webHost = this.host + 'http'.....
}

var page = new Page();