开发者

correct way to format a javascript function

开发者 https://www.devze.com 2023-04-13 02:24 出处:网络
Just a开发者_Python百科 quick one... what\'s the correct way to format a javascript function? I see it like this:

Just a开发者_Python百科 quick one...

what's the correct way to format a javascript function?

I see it like this:

function doThis(){
}

and like this:

doThis = function(){
}

Or maybe it make no difference. Please let me know whats best or if they both have different rasons or purposes.

Cheers

C


They are two different things, although they both create a function (and assign it to a variable).

function name () {
}

Is a function-statement (or "function declaration"). It is only legal to appear as a top-level script element or directly as an element of a function: that is, it is not legal for a function-statement to appear inside an if, or while, etc. All function statements are "lifted" to the top of the function (or script), thus the following is valid:

a()
function a () { alert("a") }

In the form:

variable = function name () {} // or variable = function () {}

The function keyword is in a function-expression context: it creates a new function-object and the resulting function-object (just a "normal value") is assigned to variable. The following is not valid because function-expressions are not lifted.

var b
b() // oops, does not evaluate to a function-object yet!
b = function b () { alert("b") }

All that being said, the "correct way" is to use the function-statement ("function declaration") form unless there is reason to do otherwise.

Happy coding.


See also:

  • What is the difference between a function expression vs declaration in JavaScript?
  • Function Declarations vs. Function Expressions


There is an important and also useful difference between those syntaxes.

Encapsulation

In OOP it is very useful to use encapsulation, which is a mechanism for restricting access to other objects. The difference between public and private vars/functions in javascript could stated like this:

function Color(value)
{
    // public variable
    this.value = value; // get from arguments

    // private variable
    var _name = "test";

   // public function
   this.getRandomColor = function( )
   {
     return Math.random() * 0xFFFFFF;
   }

   // private function
   function getNiceColor()
   {
     return 0xffcc00;
   }
}

Testing public

The public variables and functions inside the a color-instance are accessible:

// create instance of Color
var color = new Color(0xFF0000);
alert( color.value ); // returns red color
alert( color.getRandomColor() ); // returns random color

Testing privates

Private variables and functions cannot be accessed from the color-instance:

var color = new Color(0x0000FF); 
alert( color.getNiceColor() ); // error in console; property does not exist, because function is private.
alert( color._name ); // error in console; property does not exist, because variable is private.

NOTE
It could be better to use good-old prototypes when using public functions, because this method of coding could cause problems with inheritence.


Check out the first some 10-15 slides from [1], they talk about this

[1] http://ejohn.org/apps/learn/

0

精彩评论

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