javascript面向对象问题

javascript面向对象问题,javascript,jquery,oop,Javascript,Jquery,Oop,我有一个像这样的html页面 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="JScript2.js" type="text/javascript"></script> <script src="JScript.js" type="text/javascript"></script> <title></t

我有一个像这样的html页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="JScript2.js" type="text/javascript"></script>
    <script src="JScript.js" type="text/javascript"></script>
    <title></title>
</head>
<body >
    <div ><input type=text id="uxTextBox" /></div>
</body>
</html>
/// <reference path="JScript.js"/>
/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />

$(document).ready(function() {
    referral.PopulateTextBox();

}

和2个类似的javascript文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="JScript2.js" type="text/javascript"></script>
    <script src="JScript.js" type="text/javascript"></script>
    <title></title>
</head>
<body >
    <div ><input type=text id="uxTextBox" /></div>
</body>
</html>
/// <reference path="JScript.js"/>
/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />

$(document).ready(function() {
    referral.PopulateTextBox();

}
//
/// 
/// 
$(文档).ready(函数(){
propulatetextbox();
}
=======第一个文件的结束==============文件

/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />


var referral = new Referral(); 
$(document).ready(function() {
function Referral() {
    this.PopulateTextBox = function() {
    $("#uxTextBox").text("some text");


    }
}

}
//
/// 
var reflection=新的reflection();
$(文档).ready(函数(){
函数引用(){
this.PopulateTextBox=函数(){
$(“#uxTextBox”).text(“一些文本”);
}
}
}
问题是这两个jquery文件似乎都没有执行。我正在尝试从调用另一个js文件填充一个对象,然后将值返回到html文件


有什么想法吗?

您需要确保在html页面上包含jQuery源代码。“参考”注释看起来像是由编辑器插入的,这样它就可以知道您正在使用哪些文件,但这些注释不会告诉浏览器从何处获取jQuery源代码

将其作为文档中的第一个
标记:

<script src="/path/to/jquery.js"></script>

这是关于作用域的,JavaScript有函数作用域,因此在
$(document).ready
回调函数中执行的所有变量和函数声明都只能在该作用域中访问

例如:

$(document).ready(function() {

  function Referral() {
    // ...
  }

  // Referral is accessible here
});
// But not here
您可以在全局范围内声明您的
引用
,因为您打算从多个文件中使用它

如果您不喜欢globals,可以实现名称空间技术:

  • (精彩文章,多种选择)
  • (模块模式方法)
有关范围的更多信息: