面向对象的 JavaScript 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14972622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object oriented JavaScript with example
提问by testndtv
I am aware of general Object-oriented programming principles like Encapsulation, Inheritance, Polymorphism, Abstraction, etc
我知道一般的面向对象编程原则,如封装、继承、多态、抽象等
But would now want to understand these concepts from a JavaScript perspective. Could someone take a very basic example and run how these works in JS context (Encapsulation, Inheritance, Polymorphism, Abstraction)
但现在想从 JavaScript 的角度理解这些概念。有人可以举一个非常基本的例子并运行它们在 JS 上下文中是如何工作的(封装、继承、多态、抽象)
I have read a lot about these online, but the articles have left me more confused.
我在网上阅读了很多关于这些的文章,但这些文章让我更加困惑。
Thank you.
谢谢你。
采纳答案by Ankush Jain
You can use a function to define a singleton object
您可以使用函数来定义单例对象
var apple = new function() {
this.type = "macintosh";
this.color = "red";
this.getInfo = function () {
return this.color + ' ' + this.type + ' apple';
};
}
use it as given below
如下所示使用它
apple.color = "reddish";
alert(apple.getInfo());
new function(){...}does two things at the same time: define a function (an anonymous constructor function) and invoke it with new.
new function(){...}同时做两件事:定义一个函数(一个匿名构造函数)并用 new 调用它。
回答by Gregory Magarshak
I will describe the Douglas Crockford Pattern to mimic the style used in Object-Oriented Programming languages. It doesn't use prototypal inheritance. As a result, it's slightly less efficient because each object has to store a reference to each method. But it's very useful for illustrative purposes.
我将描述 Douglas Crockford 模式来模仿面向对象编程语言中使用的风格。它不使用原型继承。因此,它的效率略低,因为每个对象都必须存储对每个方法的引用。但它对于说明目的非常有用。
Encapsulation:
封装:
function MyClass (a, b)
{
this.publicProperty = 1;
var _privateProperty = 2;
function _privateMethod () {
// only private methods and privileged methods can call this
};
this.publicMethod = function () {
// can access _privateProperty and call _privateMethod
};
}
MyClass.classMethod = function () {
// not tied to any instances
};
Simply create objects with var instance = new MyClass(a, b);
简单地创建对象 var instance = new MyClass(a, b);
Inheritance:
遗产:
function DerivedClass(a, b, c)
{
// must explicitly call superclass constructor, like in Java
MyClass.apply(this, arguments);
this.anotherProperty = 3;
function _anotherPrivateMethod() { };
this.publicMethod = function () {
// can access _privateProperty and call _privateMethod
};
}
DerivedClass.classMethodMethod = function ()
{
// not tied to any instances
};
Polymorphism in JavaScript is mostly replaced by Duck Typing (http://en.wikipedia.org/wiki/Duck_typing). Developers usually group methods/properties under objects, and you just test for the presence of those objects. This is how newfangles browser capabilities are detected, for instance.
JavaScript 中的多态性大多被 Duck Typing ( http://en.wikipedia.org/wiki/Duck_typing)取代。开发人员通常将方法/属性分组到对象下,您只需测试这些对象是否存在。例如,这就是检测 newfangles 浏览器功能的方式。
Abstraction is closely tied with the polymorphism - as long as something supports an interface, you don't usually care how it works underneath. Thus, you may download a Javascript library and just use it, based on its documentation.
抽象与多态性密切相关——只要某些东西支持接口,你通常不会关心它在下面是如何工作的。因此,您可以下载一个 Javascript 库并根据其文档使用它。
Hope this helps.
希望这可以帮助。
回答by guy mograbi
I think the most interesting is encapsulation as a lot of people don't use it properly.
我认为最有趣的是封装,因为很多人没有正确使用它。
Lets take a simple object as an example
让我们以一个简单的对象为例
var Person = function( firstName, lastName, isMr ) {
var prefix = isMr ? "Mr." : "Mrs." ;
this.getFullName = function() { return prefix + " "
+ firstName + " " + lastName }
}
var p = new Person ("guy","mograbi", true);
p.getFullName(); // => "Mr. guy mograbi"
// p.prefix ==> causes an error.
Inheritance- is simply extending the prototype
继承- 只是扩展原型
var Superhero = function(){
Person.call(this);
}
However proper inheritance is an issue by itself. Check out https://stackoverflow.com/a/4985616/1068746
然而,适当的继承本身就是一个问题。查看https://stackoverflow.com/a/4985616/1068746
Polymorphism- is quite simple, given a new prototype that implements "getFullName"
多态- 非常简单,给定一个实现“getFullName”的新原型
var Child = function(){
this.getFullName = function(){ return "not allowed to talk with strangers" }
}
given function(a){ a.getFullName() }You will get full name if a is Person and "not allowed.." if a is Child.
鉴于function(a){ a.getFullName() }您将获得全名,如果是个人和“不允许。”如果是儿童。
Abstraction- is more of a type-safe language thing. https://stackoverflow.com/a/4082496/1068746
抽象- 更像是一种类型安全的语言。 https://stackoverflow.com/a/4082496/1068746
回答by Sumit Singh
Take a look for following links:
查看以下链接:

