Using `var` or not(使用`var`或不使用)
You should introduce any variable with the var
statement, otherwise it gets to the global scope.
(您应该使用var
语句引入任何变量,否则它将进入全局范围。)
It's worth mentioning that in strict mode ( "use strict";
) undeclared variable assignments throws ReferenceError
.
(值得一提的是,在严格模式下( "use strict";
) 未声明的变量赋值会抛出ReferenceError
。)
At present JavaScript does not have a block scope.
(目前JavaScript没有块范围。)
The Crockford school teaches you to put var statements at the beginning of the function body , while Dojo's Style Guide reads that all variables should be declared in the smallest scope possible .(Crockford学校教你将var语句放在函数体的开头 ,而Dojo的样式指南则读取所有变量应该在尽可能小的范围内声明 。)
(The let
statement and definition introduced in JavaScript 1.7 is not part of the ECMAScript standard.)((JavaScript 1.7中引入的let
语句和定义不是ECMAScript标准的一部分。))
It is good practice to bind regularly-used objects' properties to local variables as it is faster than looking up the whole scope chain.
(将常规使用的对象属性绑定到局部变量是一种好习惯,因为它比查找整个范围链更快。)
(See Optimizing JavaScript for extreme performance and low memory consumption .)((请参阅优化JavaScript以获得极高的性能和低内存消耗 。))
Defining things in the file, or in a `(function(){...})()`(定义文件中的内容,或者在`(function(){...})()`中定义)
If you don't need to reach your objects outside your code, you can wrap your whole code in a function expression—-it's called the module pattern.
(如果您不需要在代码之外访问对象,则可以将整个代码包装在函数表达式中 - 它称为模块模式。)
It has performance advantages, and also allows your code to be minified and obscured at a high level.(它具有性能优势,并且还允许您的代码在高级别上缩小和模糊。)
You can also ensure it won't pollute the global namespace.(您还可以确保它不会污染全局命名空间。)
Wrapping Functions in JavaScript also allows you to add aspect-oriented behaviour.(在JavaScript中包装函数还允许您添加面向方面的行为。)
Ben Cherry has an in-depth article on module pattern .(Ben Cherry有一篇关于模块模式的深入文章 。)
Using `this` or not(使用`this`或不使用)
If you use pseudo-classical inheritance in JavaScript, you can hardly avoid using this
.
(如果在JavaScript中使用伪古典继承,则很难避免使用this
。)
It's a matter of taste which inheritance pattern you use.(这是你使用的继承模式的品味问题。)
For other cases, check Peter Michaux's article on JavaScript Widgets Without "this" .(对于其他情况,请查看Peter Michaux关于JavaScript Widgets的文章,不要使用“this” 。)
Using `function myname()` or `myname = function();`(使用`function myname()`或`myname = function();`)
function myname()
is a function declaration and myname = function();
(function myname()
是一个函数声明, myname = function();
)
is a function expression assigned to variable myname
.(是分配给变量myname
的函数表达式。)
The latter form indicates that functions are first-class objects, and you can do anything with them, as with a variable.(后一种形式表明函数是第一类对象,你可以对它们做任何事情,就像变量一样。)
The only difference between them is that all function declarations are hoisted to the top of the scope, which may matter in certain cases.(它们之间的唯一区别是所有函数声明都被提升到范围的顶部,这在某些情况下可能很重要。)
Otherwise they are equal.(否则他们是平等的。)
function foo()
is a shorthand form.(function foo()
是一种简写形式。)
Further details on hoisting can be found in the JavaScript Scoping and Hoisting article.(有关提升的更多详细信息,请参阅JavaScript范围和提升文章。)
Defining methods in the body of the object or using "prototype"(在对象体中定义方法或使用“原型”)
It's up to you.
(由你决定。)
JavaScript has four object-creation patterns: pseudo-classical, prototypical, functional, and parts ( Crockford, 2008 ).(JavaScript有四种对象创建模式:伪古典,原型,功能和部分( Crockford,2008 )。)
Each has its pros and cons, see Crockford in his video talks or get his book The Good Parts as Anon already suggested .(每个都有它的优点和缺点, 在他的视频讲座中看到Crockford,或者得到他的书“ The Good Parts as Anon”已经提出过 。)
Frameworks(构架)
I suggest you pick up some JavaScript frameworks, study their conventions and style, and find those practices and patterns that best fit you.
(我建议您选择一些JavaScript框架,研究它们的约定和样??式,并找到最适合您的实践和模式。)
For instance, the Dojo Toolkit provides a robust framework to write object-oriented JavaScript code which even supports multiple inheritance.(例如, Dojo Toolkit提供了一个强大的框架来编写面向对象的JavaScript代码,甚至支持多重继承。)
Patterns(模式)
Lastly, there is a blog dedicated to explore common JavaScript patterns and anti-patterns .
(最后,有一个博客致力于探索常见的JavaScript模式和反模式 。)
Also check out the question Are there any coding standards for JavaScript?(另请查看问题JavaScript是否有编码标准?)
in Stack Overflow.(在Stack Overflow中。)