3

All, I am not familiar with the javascript OO, After I did some experiment with it ,I have some little confused questions about the object definition ,Please help to review my code and comments below.Thanks.

    GlobalUtility = function() {
            this.templeteCode = "123";

            this.Init();
//why can not put this code here,
//there is an error says GetOriginalSource is not a function . 
//This is not like the classical OO constructor which can call any methods in class.
            this.Init = function() {
                var source=this.GetOriginalSource();
                alert(source + " is ok.");
            },//I found I can end this statement by , or ; Is there any difference between them?

            this.GetOriginalSource=function(){
                return "abc";
            };
            //this.Init(); putting this code here is ok .

        };
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • 1
    I think that this question need to be moved to `codeReview` or `Programmers`. – Red Mar 12 '13 at 10:11
  • Hi ,@Red , Where is the codeReview and Programmers .Could you please give me a link. Next time I will follow it .thanks. – Joe.wang Mar 12 '13 at 10:13
  • http://programmers.stackexchange.com/?as=1 , http://codereview.stackexchange.com/?as=1 also check out the amazing stack sites. – Red Mar 12 '13 at 10:30

3 Answers3

4
  1. You must define a function before calling it.
  2. Semicolons in javascript are optional. Basically, a semicolon is used to end the statement while comma is when you working with objects. You could try reading these article JavaScript variable definition: Commas vs. Semicolons and Do you recommend using semicolons after every statement in JavaScript?

Javascript can be written in an oop way *see defining javascript class but i recommended to use Base.js, it will make your life easier.

You probably need this but it is not that fun to read :) javascript patterns

Community
  • 1
  • 1
lngs
  • 690
  • 1
  • 8
  • 17
  • If you are compressing your code, it is good to use semi-colon (;), hence the code could become something else, ex: var foo = 1var bar = 2; Also, when compressing, avoid // for comments, since it will comment the rest of the line to – Hiny Mar 12 '13 at 11:12
1

try this:

GlobalUtility = function () {
            Init();


            this.templeteCode = "123";
            this.Init = Init;       
            this.GetOriginalSource = GetOriginalSource;

            //function declaration
            function Init() {
                var source = GetOriginalSource();
                alert(source + " is ok.");
            }
            function GetOriginalSource() {
                return "abc";
            }
};

You're trying to call a function that is not yet defined on runtime.

Oleg Pnk
  • 332
  • 2
  • 5
0

this.GetOriginalSource=function(){ adds the function to the object. It is not there before this line. But you try to call it before.

a better oliver
  • 26,330
  • 2
  • 58
  • 66