0

Ok, I asked a question, and someone smarter than me said the question was an "exact" duplicate of mine. Well it wasn't, and the answers did not answer the guy's question. He wanted to know how to call another procedure, I want to know how to call a function within the class. That being said when I was trying to find an answer to my problem and I was looking at answers to similar questions sometimes function were terminated with }; other times with } and still other times with }, (this one I think I understand). However I cannot see in rhyme or reason why or when the brace with a semicolon is to be used and when just the brace should be used:

Here was the first answer

var ExampleClass = function(){
    this._m_a = null;
};
ExampleClass.prototype.get_m_a = function(){
    return this._m_a;
};
ExampleClass.prototype.set_m_a = function(value){
   this._m_a = value;
}; 

notice this answer doesn't even address the calling of another routine! However the functions are terminated with a }; (Why?)

Here is another answer:

var M_A = function()
{
    var m_a; //private variable

    this.get = function()
    {
         return m_a;
    }

    this.set = function(value)
    {
         m_a = value;
         RunFunction();    //run some global or private function
         this.runPublic(); // run a public function
    }
}

This guy kinda answers the question but all of the functions are terminated with a } with no semicolon following (Why?)

Finally the answer is unsatisfactory, What is a global or private function? and what is a public function? Why does the public function refer to "this". Anyhow I am probably the village idiot but I still do not know how to call a function I have defined in the class from within the class, and I don't know whether the termination of functions inside a class should be with a }; or just a }. So I have now spent almost 12 hours trying to get one little class to work in javascript to no avail.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    A *function Declaration* (or function statement) never needs a `; while` a *function Expression* might when ASI (*automatic semicolon insertion*) does not hold (eg. next line starts with a `(` or `+`); but a number of people (and jshint) ***recommend*** terminating all statements/lines (except those *statements* - eg. if/while/function - that naturally terminate with a `}`) with a semicolon. Look for these keywords in https://es5.github.io/ and on SO. – user2864740 May 07 '15 at 03:06
  • 1
    Please ask one specific question per post, and leave out the meta stuff about any other questions you may have asked. – user229044 May 07 '15 at 03:16
  • 1
    Charlie, welcome to SO. Could you please post your code that you're having a problem with? You're referring to answers and questions without links to the questions/answers. SO is sorted various ways, but hardly ever sequentially. If you have a problem getting the link, click the "share" button under the question/answer to retrieve a URL that you can copy/paste. – vol7ron May 07 '15 at 03:16
  • Well never mind I am going to try to edit my other question which was also listed as duplicate. It has been a long day for me. Thank you for your kind responses. – Charlie Elliot May 07 '15 at 03:45

1 Answers1

3

Two things are tripping you up.

The first one is the difference between function declarations and function values.

This is a function declaration:

function foo() {
  // ...
}

These are function values:

var bar = function() {
  // ...
};

var bar = function foo() {
  // ...
};

baz(function() {
  // ...
});

Function declarations do not need semicolons. Function values are not full statements; full statements do need them (sometimes). It is important to note that in these last three examples, the semicolon does not terminate the function, it terminates the var statement and the function evaluation statement.

This "(sometimes)" is the second point where you have a problem: in JavaScript, a semicolon will be automatically inserted at newline if it is obvious to the interpreter that it should be there.

So,

x = 5
y = 6

is identical to

x = 5;
y = 6;

Thus,

var bar = function() {
  // ...
}

is identical to

var bar = function() {
  // ...
};

But look at this:

var bar = function() { /* ... */ }; console.log(bar);

is okay (all needed semicolons are there);

function bar() { /* ... */ } console.log(bar);

is also okay, since the function declaration statement does not need a semicolon. However,

var bar = function() { /* ... */ } console.log(bar);

is an error, as the variable assignment statement cannot be separated properly from the function invocation statement. Note that due to automatic semicolon insertion, this works:

var bar = function() { /* ... */ }
console.log(bar);

EDIT: There is a semantic difference between a function declaration statement and assigning a function value to a variable: function declaration statements are executed first (this is called "function hoisting"), while assignments are, as all assignments, executed in order:

function pre_fn() {}
var pre_var = function() {};

pre_fn(); // works
pre_var(); // works
post_fn(); // works
post_var(); // error (not defined yet)

function post_fn() {}
var post_var = function() {};

EDIT2:

I suspect that the class that you were unable to create (as said in comments, code in the question would have been better than speculation) had an object literal form:

var Foo = {
  bar: function() {
    // ...
  },
  baz: function() {
    // ...
  }
};

Here, the semicolon terminates the var Foo = { /* ... */ } statement. You can't have semicolons inside it, for the same reason you can't write

var foo = { a = 6; b = 7 }

because the syntax of the object literal mandates that you separate the key-value pairs with commas. There is no difference here between the numeric value 6, and the function value function() { /* ... */ }; neither "terminates" with a semicolon.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thank you Thank you Thank you wow, that even makes sense! The next time you have that job review have your boss call me, i'll get you that raise!!! – Charlie Elliot May 07 '15 at 03:43