简体   繁体   English

格式化javascript函数的正确方法

[英]correct way to format a javascript function

Just a quick one... 快一点......

what's the correct way to format a javascript function? 什么是格式化javascript函数的正确方法?

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. 请让我知道什么是最好的,或者他们都有不同的rasons或目的。

Cheers 干杯

C 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: 它是唯一合法的出现为顶级脚本元素或直接作为函数的元素:即,它是不合法的函数声明内部的出现if ,或while ,等所有功能声明“提升“到函数(或脚本)的顶部,因此以下是有效的:

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 . function关键字位于函数表达式上下文中:它创建一个新的函数对象,并将得到的函数对象(只是一个“正常值”)赋给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: 也可以看看:

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. 在OOP中,使用封装非常有用,封装是一种限制对其他对象的访问的机制。 The difference between public and private vars/functions in javascript could stated like this: javascript中公共和私有变量/函数之间的区别可以这样说:

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: 无法从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]中的前10-15张幻灯片,他们谈论这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM