Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

javascript - 面向对象的Javascript最佳实践? [关闭](Object Oriented Javascript best practices? [closed])

I'm finding myself coding a big project in Javascript.

(我发现自己用Javascript编写了一个大项目。)

I remember the last one was quite an adventure because hacky JS can quickly becomes unreadable and I want this code to be clean.

(我记得最后一次是冒险,因为hacky JS很快就变得难以理解,我希望这段代码干净利落。)

Well, I'm using objects to construct a lib, but there are several ways to define things in JS, implying important consequences in the scope, the memory management, the name space, etc. EG :

(好吧,我正在使用对象来构造一个lib,但是有几种方法可以在JS中定义东西,这意味着在范围,内存管理,名称空间等方面会产生重要影响.EEP:)

  • using var or not;

    (使用var或不使用;)

  • defining things in the file, or in a (function(){...})() , jquery style;

    (定义文件中的内容,或者在(function(){...})() ,jquery样式中定义;)

  • using this , or not;

    (使用this ,不是;)

  • using function myname() or myname = function() ;

    (使用function myname()myname = function() ;)

  • defining methods in the body of the object or using "prototype";

    (定义对象主体中的方法或使用“原型”;)

  • etc.

    (等等)

So what are really the best practices when coding in OO in JS ?

(那么在JS中用OO编码时,最佳实践是什么?)

Academic explanations really expected here.

(学术解释在这里真的很期待。)

Link to books warmly welcome, as long as they deal with quality and robustness.

(链接到书籍热烈欢迎,只要他们处理质量和稳健性。)

EDIT :

(编辑:)

Got some readings, but I'm still very interested in answers to the questions above and any best practices.

(有一些阅读,但我仍然对上述问题的答案和任何最佳实践非常感兴趣。)

  ask by e-satis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...