开发者

javascript oo question

I have a html page like this

<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开发者_如何学C="uxTextBox" /></div>
</body>
</html>

and 2 javascript files like this

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

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

}

=====end of first========== file

/// <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");


    }
}

}

the problem is that neither of the 2 jquery files seem to execute. I am trying to populate an object from the call to another js files and then return the value to the html file

any ideas?


It's about the scope, JavaScript has function-scope, so all the variables and function declarations that you do inside the $(document).ready callback function, are only accessible in that scope.

For example:

$(document).ready(function() {

  function Referral() {
    // ...
  }

  // Referral is accessible here
});
// But not here

You could declare your Referral constructor function in the global scope, as you intend to use it from multiple files.

And if you don't like globals you can implement a namespacing technique:

  • JavaScript Namespacing (great article, many alternatives)
  • Namespacing your JavaScript (module pattern approach)

More info about the scope:

  • Functions and function scope
  • Functions


You'll need to make sure to include the jQuery source on your html page. The "reference" comments look like they are something inserted by your editor so it can figure out which files you are using, but those comments do not tell the browser where to get the jQuery source.

Put this as the first <script> tag in your document:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜