用 Javascript 进行 OOP 编程 - 正确
我对改进我的 javascript 代码以使其成为适当的 OOP 很感兴趣......目前我倾向于做这样的事情:
jQuery(document).ready(function () {
Page.form = (function () {
return {
//generate a new PDF
generatePDF: function () {
},
//Update the list of PDFs available for download
updatePDFDownloads: function () {
},
/*
* Field specific functionality
*/
field: (function () {
return {
//show the edit prompt
edit: function (id, name) {
},
//refresh the value of a field with the latest from the database
refresh: function (id) {
}
};
}())
};
}());
});
我想最后它只是主要组织的函数......有什么好的资源可以让我学习如何对 javascript 进行编程以面向对象的方式,或者您对改进我当前的编程风格有何建议?
看来我应该做一种模型原型,并让我的 form
对象从该原型继承。
(由于与prototypeJS冲突,我使用jQuery而不是$)
I'm interesting in improving my javascript code to be properly OOP.... currently I tend to do something like this:
jQuery(document).ready(function () {
Page.form = (function () {
return {
//generate a new PDF
generatePDF: function () {
},
//Update the list of PDFs available for download
updatePDFDownloads: function () {
},
/*
* Field specific functionality
*/
field: (function () {
return {
//show the edit prompt
edit: function (id, name) {
},
//refresh the value of a field with the latest from the database
refresh: function (id) {
}
};
}())
};
}());
});
In the end it's just mainly organized functions I suppose... what's a good resource where I can learn to program javascript in an OOP manner, or what suggestions would you have for improving my current style of programming?
It seems like I should do a sort of model prototype and have my form
object inherit from that prototype.
(I'm using jQuery instead of $ because of conflicts with prototypeJS)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的问题非常广泛,因此我认为这里不可能给出完整的答案。但这里有几点。
关于您所显示的代码。你正在跳几个多余的圈。
你创建的对象可以像这样更简单地创建(一件好事)
它更容易阅读并且不那么混乱,只做那些能给你带来东西的事情。请参阅 cargo cult 编程
这是一个使用自调用匿名函数来创建私有成员的示例
您拥有的结构正如您所说,使用对象文字非常适合对一组函数(方法)和属性进行分组。这是一种命名空间。它也是创建单例的一种方法。您可能还想创建同一类的许多对象。
JavaScript 没有像传统面向对象语言那样的类(我会讲到),但在最简单的层面上,创建一个“模板”来创建特定类型的对象是非常容易的。这些“模板”是称为构造函数的普通函数。
关于构造函数有很多东西需要了解。你必须四处寻找。 SO 上有很多例子。
继承是面向对象的基础,但在 js 中并不那么直观,因为它是原型的。我不会在这里讨论这个,因为你很可能不会直接使用 js 的原生原型继承范例。
这是因为有些库可以非常有效地模仿经典继承,原型(继承) 或 mootools(类)。 有 其他。
许多人说继承在面向对象中被过度使用,并且您应该支持组合,这让我想到了我的观点当我开始这个漫无目的的答案时,最初就打算推荐。
JavaScript 中的设计模式与任何 OO 语言中的设计模式一样有用,您应该熟悉它们
我建议您阅读 专业 JavaScript 设计模式。
在那里,就是这样
Your question is quite broad so I don't think a complete answer is possible here. But here are a few points.
Regarding the code you have shown. You're jumping a couple of redundant hoops.
jQuery(document).ready()
The object you have created can be created more simply (a good thing) like this
It's easier to read and less confusing, only do things that buy you something. see cargo cult programming
Here's an example using a self calling anonymous function to create private members
The structure you have used, object literals are very good, as you say, at grouping a set of functions (methods) and properties. This is a kind of namespace. It is also a way of creating a Singleton. You may also want to create many objects of the same Class.
JavaScript doesn't have classes like traditional OO languages (I'll get to that) but at the simplest level it's very easy to create a 'template' for creating objects of a particular type. These 'templates' are normal functions called constructors.
There's a lot to know about constructors. You'll have to search around. There are lots of examples on SO.
Inheritance, the foundation of OO, is not that intuitive in js because it is prototypal. I won't go into that here because you will more than likely not use js's native prototypal inheritance paradigm directly.
This is because there are libraries that mimic classical inheritance very effectively, Prototype (inheritance) or mootools (Class) for example. There are others.
Many say that inheritance is overused in OO and that you should favour composition and this brings me to what I initially set out to recommend when I started this rambling answer.
Design patterns in JavaScript are as useful as in any OO language and you should familiarise yourself with them
I recommend you read Pro JavaScript Design Patterns.
There, that's it
面向对象的 JavaScript 和一般 JavaScript 的一些很好的资源...
在线文章
书籍
我希望这会有所帮助。
赫里斯托
Some good sources for Object-Oriented JavaScript and JavaScript in general...
Online Articles
Books
I hope this helps.
Hristo
没有一种正确的方法......有些人使用框架来定义他们的对象,我喜欢直接扩展原型。不管怎样,我想说 Oran Looney 有一些关于 JS 中的 OO 机制的好帖子:
http:// /oranlooney.com/classes-and-objects-javascript/
也值得看看他的其他文章:
http://oranlooney.com/deep-copy-javascript/
http://oranlooney.com/function-javascript/
There isn't one correct way... Some people use a framework to define their object, I like to just extend prototype directly. Anyhow, I wanted to say that Oran Looney has some good posts on OO mechanics in JS:
http://oranlooney.com/classes-and-objects-javascript/
Its also worth looking at his other articles:
http://oranlooney.com/deep-copy-javascript/
http://oranlooney.com/functional-javascript/
我建议阅读的前 3 本书是
JavaScript 和面向对象编程 (OOP)
JavaScript 中的经典继承
原型继承JavaScript 中的原型继承
祝您阅读愉快!
The top 3 I suggest to read is
JavaScript and Object Oriented Programming (OOP)
Classical Inheritance in JavaScript
Prototypal Inheritance in JavaScript
Have a nice reading!
我们使用的代码遵循这个基本结构:
这样做的优点是它自动初始化 Global 对象,允许您维护代码的完整性,并根据您的定义将每个功能组织到特定的分组中。这个结构很扎实,呈现了您期望从 OOP 中获得的所有基本语法内容,而无需关键字。甚至可以使用 javascript 设置智能感知,然后定义每个部分并引用它们使得编写 javascript 更干净且更易于管理。希望这个布局有帮助!
The code we are using follows this basic structure:
The strength to this is that it initializes the Global object automatically, allows you to maintain the intergrity of your code, and organizes each piece of functionality into a specific grouping by your definition. This structure is solid, presenting all of the basic syntactical things you would expect from OOP without the key words. Even setting up intelisense is possible with javascript, and then defining each peice and referencing them makes writing javascript cleaner and more manageable. Hope this layout helps!
我认为你使用什么语言并不重要,好的 OOP 就是好的 OOP。我喜欢通过使用 MVC 架构来尽可能地分散我的关注点。由于 JavaScript 是基于事件的,所以我也主要使用观察者设计模式。
这里是一个有关使用 jQuery 的 MVC 的教程。
I dont think it matters what language you use, good OOP is good OOP. I like to split up my concerns as much as possible by using an MVC architecture. Since JavaScript is very event based, I also use the observer design pattern mostly.
Heres a tutorial you can read about MVC using jQuery.